[R] Confusing concept of vector and matrix in R

Gabor Grothendieck ggrothendieck at gmail.com
Tue Mar 30 04:48:32 CEST 2010


Actually R looks at it the other way around. It regards a matrix as a
special case of a vector.  A vector has no dimensions.  A vector with
dimensions is an array.  An array with two dimensions is a matrix.

Try using drop=FALSE like this:

m <- matrix(1:6, 3)
m[, 2, drop = FALSE]

On Mon, Mar 29, 2010 at 7:13 PM, yehengxin <xye78 at hotmail.com> wrote:
>
> 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!
> --
> View this message in context: http://n4.nabble.com/Confusing-concept-of-vector-and-matrix-in-R-tp1707170p1707170.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list