[R] Confusing concept of vector and matrix in R

Steve Lianoglou mailinglist.honeypot at gmail.com
Tue Mar 30 04:47:42 CEST 2010


> Why does R need the concept of "Vector"?  In my opinion, it is a useless and
> confusing concept.  A vector is simply a special case of a matrix whose row
> or column number is equal to 1.  When I take submatrix from one matrix and
> if row or column number is 1, R will automatically convert it into a vector.
> It is very straightforward that a submatrix of a matrix should be a matrix.
> In each time, I have to use as.matrix() to convert the vector back to
> matrix.    It is very annoying!

Good thing there's a way to get around this then:

R> m <- matrix(1:20, 4, 5)
R> m
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    5    9   13   17
[2,]    2    6   10   14   18
[3,]    3    7   11   15   19
[4,]    4    8   12   16   20

R> m[,1]
[1] 1 2 3 4

R> m[,1,drop=FALSE]
     [,1]
[1,]    1
[2,]    2
[3,]    3
[4,]    4

R> is.matrix(m[,1,drop=FALSE])
[1] TRUE

-steve

-- 
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact



More information about the R-help mailing list