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:> 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 FernandezYou 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:> 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 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
You can also use the comment() function to add comments to any kind of R object. Matthieu Dubois
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:>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 > >______________________________________________ >R-help at r-project.org mailing list >https://*stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide http://*www.*R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062
One way to add the text is to use a list with named elements for the row and column names:> (M <- matrix(1:9, ncol=3, byrow=TRUE,+ dimnames=list(" "=LETTERS[1:3], + "THIS IS AN EXAMPLE\nOF a 3x3 MATRIX"=LETTERS[1:3])) ) THIS IS AN EXAMPLE OF a 3x3 MATRIX A B C A 1 2 3 B 4 5 6 C 7 8 9 Larry Hotchkiss --------------------------------- Original Post ----------------------------- Message: 7 Date: Thu, 14 Jan 2010 11:04:27 +0100 From: <carferper at alum.us.es> To: r-help at r-project.org Subject: [R] To add text in a matrix Message-ID: <f61eaf1f1d8d7.4b4efa3b at us.es> Content-Type: text/plain; charset=us-ascii 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