[R] To add text in a matrix

(Ted Harding) Ted.Harding at manchester.ac.uk
Thu Jan 14 12:55:26 CET 2010


On 14-Jan-10 10:04:27, carferper at alum.us.es wrote:
> Dear colleagues,
> 
> I would need to add text (some rows of information) in a matrix.
> For example, given this matrix
> 
> 1 2 3
> 4 5 6
> 7 8 9
> 
> I would need to add this info:
> 
> THIS IS AN EXAMPLE
> OF a 3x3 MATRIX
> 1 2 3
> 4 5 6
> 7 8 9
> 
> I have been looking for a function that works similar to "fopen"
> in matlab, but unfortunately I have not found It in R.
> 
> Thank you in advance for your help!
> Carlos Fernandez

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 ------------------------------



More information about the R-help mailing list