Skip to content
Prev 206306 / 398503 Next

To add text in a matrix

On 14-Jan-10 10:04:27, carferper at alum.us.es wrote:
You cannot mix data types (in this case character and numeric)
in a matrix (and in any case, even if you could, your text would
become an element of the matrix itself, which presumably you
would not want).

One way to do this is to make a list, one element being the
text "metadata", the other the matrix itself:

  M <- list(Meta="THIS IS AN EXAMPLE OF a 3x3 MATRIX",
            Matrix=matrix(c(1,2,3,4,5,6,7,8,9),byrow=TRUE,ncol=3))

  M
  # $Meta
  # [1] "THIS IS AN EXAMPLE OF a 3x3 MATRIX"
  # $Matrix
  #      [,1] [,2] [,3]
  # [1,]    1    2    3
  # [2,]    4    5    6
  # [3,]    7    8    9

and you can access either element using

  M$Meta
  M$Matrix

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 14-Jan-10                                       Time: 11:55:22
------------------------------ XFMail ------------------------------