[R] convert vector to matrix
Hi All, I have vector containing 10 elements(odservations). I need to put this into a matrix, where the matrix should contain only one column. I need this to be done using the cbind command because in the final table I should have a column of attributes( from adifferent vector) and a column of vectors from the matrix above Please suggest me how this can be done Thanks and Regards, Pratap - [[alternative HTML version deleted]] __ R-help@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.
Re: [R] convert vector to matrix
On Wed, 2006-10-04 at 21:50 +0100, nalluri pratap wrote: Hi All, I have vector containing 10 elements(odservations). I need to put this into a matrix, where the matrix should contain only one column. I need this to be done using the cbind command because in the final table I should have a column of attributes( from adifferent vector) and a column of vectors from the matrix above Please suggest me how this can be done Thanks and Regards, Pratap Did you even try to use cbind on your vector? I guess not as, vec - 1:10 mat - cbind(vec) mat vec [1,] 1 [2,] 2 [3,] 3 [4,] 4 [5,] 5 [6,] 6 [7,] 7 [8,] 8 [9,] 9 [10,] 10 class(mat) [1] matrix clearly shows it is possible. G -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% *Note new Address and Fax and Telephone numbers from 10th April 2006* %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [t] +44 (0)20 7679 0522 ECRC [f] +44 (0)20 7679 0565 UCL Department of Geography Pearson Building [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street London, UK[w] http://www.ucl.ac.uk/~ucfagls/cv/ WC1E 6BT [w] http://www.ucl.ac.uk/~ucfagls/ %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% __ R-help@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.
Re: [R] convert vector to matrix
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@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.
Re: [R] convert vector to matrix
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 [EMAIL PROTECTED] 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@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 __ R-help@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.
Re: [R] convert vector to matrix
On Wed, 2006-10-04 at 19:55 -0200, Alberto Monteiro 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. Dear Alberto, That FAQ is referring to subsetting a matrix, and as yet, the code above has not subsetted mat at all - indeed, we have only created it and then examined it's class, if we were to now look at rows 2:5, then we'd see that mat loses a dimension and reverts to a vector: vec - 1:10 mat - cbind(vec) mat vec [1,] 1 [2,] 2 [3,] 3 [4,] 4 [5,] 5 [6,] 6 [7,] 7 [8,] 8 [9,] 9 [10,] 10 mat[2:5,] [1] 2 3 4 5 vec[2:5] [1] 2 3 4 5 class(mat[2:5,]) [1] integer class(vec[2:5]) [1] integer all.equal(mat[2:5,], vec[2:5]) [1] TRUE To preserve dimensions, we use drop = FALSE, as discussed in the FAQ you quote: mat[2:5, , drop = FALSE] vec [1,] 2 [2,] 3 [3,] 4 [4,] 5 class(mat[2:5, , drop = FALSE]) [1] matrix HTH G -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% *Note new Address and Fax and Telephone numbers from 10th April 2006* %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [t] +44 (0)20 7679 0522 ECRC [f] +44 (0)20 7679 0565 UCL Department of Geography Pearson Building [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street London, UK[w] http://www.ucl.ac.uk/~ucfagls/cv/ WC1E 6BT [w] http://www.ucl.ac.uk/~ucfagls/ %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% __ R-help@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.