Skip to content

To add text in a matrix

5 messages · carferper at alum.us.es, (Ted Harding), Henrique Dallazuanna +2 more

#
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
#
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 ------------------------------
#
You can use the Matrix package:

Matrix::Matrix(1:9, ncol = 3)
On Thu, Jan 14, 2010 at 8:04 AM, <carferper at alum.us.es> wrote:

  
    
#
In R, see   ?connection    for information about functions similar to 
matlab's fopen.

If what you're trying to do is write information and the matrix to a 
file, then you can at least get started with something along the 
lines of:

   cat('This is my information\nand a second line of it\n',file='myfile')
   write.table(mymatrix, 'myfile',append=TRUE)

See also the sink() function

-Don
At 11:04 AM +0100 1/14/10, <carferper at alum.us.es> wrote: