# [R] sub() of matrix returns a vector and not a matrix

Wolfram Fischer - Z/I/M wolfram at fischer-zim.ch
Fri May 3 15:16:25 CEST 2002

```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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```