[R] convert vector to matrix
David Barron
mothsailor at googlemail.com
Thu Oct 5 00:14:37 CEST 2006
cbind always produces a matrix or data frame. What the FAQ says is
that a subsetting operation *on* a matrix will result in redundant
dimensions being
dropped, not that you can't get a matrix with a "redundant" dimension
as a result of a function.
So,
> vec<-1:10
> vec
[1] 1 2 3 4 5 6 7 8 9 10
> cbind(vec)
vec
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5
[6,] 6
[7,] 7
[8,] 8
[9,] 9
[10,] 10
> mat<-cbind(vec)
> dim(mat)
[1] 10 1
> mat[,1]
[1] 1 2 3 4 5 6 7 8 9 10
takes us back to a vector again.
On 04/10/06, Alberto Monteiro <albmont at centroin.com.br> wrote:
> Gavin Simpson wrote:
> >
> > Did you even try to use cbind on your vector? I guess not as,
> >
> > > vec <- 1:10
> > > mat <- cbind(vec)
> > > mat
> >
> Why does mat remain a matrix instead of "losing" one dimension?
> I am reading the FAQ at...
> http://cran.us.r-project.org/doc/FAQ/R-FAQ.html#Why-do-my-matrices-lose-
> dimensions_003f
> ... and it seems that anything you do to mat should turn it back
> into a one-dimensional vector.
>
> Alberto Monteiro
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
--
=================================
David Barron
Said Business School
University of Oxford
Park End Street
Oxford OX1 1HP
More information about the R-help
mailing list