Wolfram Fischer - Z/I/M
2002-May-03 13:16 UTC
[R] sub() of matrix returns a vector and not a matrix
Is there a simple possibility to become directly a matrix from a call of sub() on a matrix? --------- START OF LOGFILE ---------------- # R 1.4.1> a <- matrix( letters[1:6], 2, 3 )# a is a matrix> print( a )[,1] [,2] [,3] [1,] "a" "c" "e" [2,] "b" "d" "f"> b <- sub( '(.)', '-\\1-', a )# I had expected b to be a matrix # but b is a vector> print( b )[1] "-a-" "-b-" "-c-" "-d-" "-e-" "-f-"> dim(b) <- dim(a) > print( b )# b is now a matrix again [,1] [,2] [,3] [1,] "-a-" "-c-" "-e-" [2,] "-b-" "-d-" "-f-" --------- END OF LOGFILE ---------------- Wolfram Fischer -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Wolfram Fischer - Z/I/M
2002-May-04 05:53 UTC
[R] sub() of matrix returns a vector and not a matrix
Thanks, Andy, for this hint which is easy to implement. I am just wundering now if there is a reason for this loss of information about the data structure or if it is only an inconsistant implementation of a good idea about matrix operations: MATRIX * 10 results in a MATRIX paste( MATRIX, "." ) results in a VECTOR sub( "a", "A", as.character(MATRIX) ) results in a VECTOR Wolfram Fischer -----Original Message----- From: Liaw, Andy [mailto:andy_liaw at merck.com] Sent: Friday, May 03, 2002 3:56 PM To: 'Wolfram Fischer - Z/I/M' Subject: RE: [R] sub() of matrix returns a vector and not a matrix `Matrix' in R is just a vector with `dim' attribute. The sub() function (and many others) take a vector as input, and returns a vector. It does not copy any attributes from the input to the output. You can just as easily write a simple wrapper function that adds the dim(b) <- dim(a) operation. Andy> -----Original Message----- > From: Wolfram Fischer - Z/I/M [mailto:wolfram at fischer-zim.ch] > Sent: Friday, May 03, 2002 9:16 AM > To: r-help at stat.math.ethz.ch > Subject: [R] sub() of matrix returns a vector and not a matrix > > > Is there a simple possibility > to become directly a matrix > from a call of sub() on a matrix? > > > --------- START OF LOGFILE ---------------- > # R 1.4.1 > > > a <- matrix( letters[1:6], 2, 3 ) > # a is a matrix > > print( a ) > [,1] [,2] [,3] > [1,] "a" "c" "e" > [2,] "b" "d" "f" > > > b <- sub( '(.)', '-\\1-', a ) > # I had expected b to be a matrix > # but b is a vector > > print( b ) > [1] "-a-" "-b-" "-c-" "-d-" "-e-" "-f-" > > > > dim(b) <- dim(a) > > print( b ) > # b is now a matrix again > [,1] [,2] [,3] > [1,] "-a-" "-c-" "-e-" > [2,] "-b-" "-d-" "-f-" > > --------- END OF LOGFILE ---------------- > > Wolfram Fischer-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Martin Maechler
2002-May-07 14:25 UTC
[R] sub() of matrix returns a vector and not a matrix
>>>>> "Wolfram" == "Wolfram Fischer <- Z/I/M" <wolfram at fischer-zim.ch>> writes:Wolfram> Is there a simple possibility to become directly a Wolfram> matrix from a call of sub() on a matrix? This can be considered as a feature proposal which I would second: sub() and gsub() could be enhanced such as to keep attributes(.) of their x argument. The `dim' attribute which makes a vector into a matrix is just one kind of attributes. Opinions? >> a <- matrix( letters[1:6], 2, 3 ) >> b <- sub( '(.)', '-\\1-', a ) Wolfram> # I had expected b to be a matrix # but b is a vector >> print( b ) Wolfram> [1] "-a-" "-b-" "-c-" "-d-" "-e-" "-f-" >> dim(b) <- dim(a) >> print( b ) Wolfram> # b is now a matrix again .... Martin Maechler <maechler at stat.math.ethz.ch> http://stat.ethz.ch/~maechler/ Seminar fuer Statistik, ETH-Zentrum LEO C16 Leonhardstr. 27 ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND phone: x-41-1-632-3408 fax: ...-1228 <>< -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._