[R] sub() of matrix returns a vector and not a matrix
Wolfram Fischer - Z/I/M
wolfram at fischer-zim.ch
Sat May 4 07:53:07 CEST 2002
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list