Re: [R] Vector to Matrix transformation
non-elegant solution: matrix(c(V1,rep(NA,-length(V1)%%5)),nrow=5) HTH. On 1/24/07, Shubha Vishwanath Karanth <[EMAIL PROTECTED]> wrote: > How to suppress the recycling of items in a matrix..instead NA can be > filled. > -Original Message- > From: Chuck Cleland [mailto:[EMAIL PROTECTED] > Sent: Tuesday, January 23, 2007 8:00 PM > To: Shubha Vishwanath Karanth > Cc: r-help@stat.math.ethz.ch > Subject: Re: [R] Vector to Matrix transformation > > Shubha Vishwanath Karanth wrote: > > Hi R, > > > > I have a vector V1 of unknown length, say n. I need to convert this > into > > a matrix C of row size=5, and accordingly the column should be > updated. > > I tried with: > > > > C=as.matrix(V1,5,n/5) > > > > But it is not working...Could somebody help me on this? > > You could try the following: > > matrix(V1, nrow=5) > > but note what happens when the length of V1 is not a multiple of 5. > > > Thanks in advance... > > > > > > > > > > [[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. > > -- > Chuck Cleland, Ph.D. > NDRI, Inc. > 71 West 23rd Street, 8th floor > New York, NY 10010 > tel: (212) 845-4495 (Tu, Th) > tel: (732) 512-0171 (M, W, F) > fax: (917) 438-0894 > > __ > 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. > __ 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] Vector to Matrix transformation
How to suppress the recycling of items in a matrix..instead NA can be filled. -Original Message- From: Chuck Cleland [mailto:[EMAIL PROTECTED] Sent: Tuesday, January 23, 2007 8:00 PM To: Shubha Vishwanath Karanth Cc: r-help@stat.math.ethz.ch Subject: Re: [R] Vector to Matrix transformation Shubha Vishwanath Karanth wrote: > Hi R, > > I have a vector V1 of unknown length, say n. I need to convert this into > a matrix C of row size=5, and accordingly the column should be updated. > I tried with: > > C=as.matrix(V1,5,n/5) > > But it is not working...Could somebody help me on this? You could try the following: matrix(V1, nrow=5) but note what happens when the length of V1 is not a multiple of 5. > Thanks in advance... > > > > > [[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. -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 __ 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] Vector to Matrix transformation
Shubha Vishwanath Karanth wrote: > Hi R, > > I have a vector V1 of unknown length, say n. I need to convert this into > a matrix C of row size=5, and accordingly the column should be updated. > I tried with: > > C=as.matrix(V1,5,n/5) > > But it is not working...Could somebody help me on this? You could try the following: matrix(V1, nrow=5) but note what happens when the length of V1 is not a multiple of 5. > Thanks in advance... > > > > > [[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. -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 __ 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] Vector to Matrix transformation
check the help page for ?matrix(); you probably want either matrix(V1, nrow = 5) or matrix(V1, nrow = 5, byrow = TRUE) I hope it helps. Best, Dimitris Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm - Original Message - From: "Shubha Vishwanath Karanth" <[EMAIL PROTECTED]> To: Sent: Tuesday, January 23, 2007 3:10 PM Subject: [R] Vector to Matrix transformation > Hi R, > > > > I have a vector V1 of unknown length, say n. I need to convert this > into > a matrix C of row size=5, and accordingly the column should be > updated. > I tried with: > > > > C=as.matrix(V1,5,n/5) > > > > But it is not working...Could somebody help me on this? > > > > Thanks in advance... > > > > > [[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. > Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm __ 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] Vector to Matrix transformation
Shubha Vishwanath Karanth wrote: > Hi R, > > > > I have a vector V1 of unknown length, say n. I need to convert this into > a matrix C of row size=5, and accordingly the column should be updated. > I tried with: > > > > C=as.matrix(V1,5,n/5) > > > > But it is not working...Could somebody help me on this? matrix(), not as.matrix() The help page for as.matrix displays quite clearly that as.matrix takes only a single argument x. You can also set the dimensions using dim(V1) <- c(5, n/5) > > > Thanks in advance... > > > > > [[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. > -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ 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] vector to matrix transformation
If v is a vector, as.matrix(v) and t(v) give row and column matrices and matrix(v,nrow=nr, ncol=nc) gives a matrix with nr rows and nc columns such that the vector fills the first column, then the second, etc. You only have to provide nr or nc in most cases. If m is a matrix c(m) is a vector formed by stringing out the columns one after another. as.vector is similar. Date: Fri, 3 Dec 2004 11:39:57 +0100 From: Gwenael Jacob <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Subject: [R] vector to matrix transformation Dear, Some analysis (linear regression) can only be done from a vectorized dataset whereas others require a matrix (Mantel tests). I use the two analyses and thus need to format my data in matrix and vector. I spent some time trying to solve the problem and I just gave up. Did anyone knows how to transform a matrix into a vector and back-transform a vector into a matrix? Thanks by advance, Gwenaël Jacob -- - Gwenaël JACOB Division Biodiversity Swiss Federal Research Institute WSL Zuercherstrasse 111/Postfach CH-8903 Birmensdorf SWITZERLAND Phone : ++41 1 7392 504 Fax : ++41 1 7392 215 __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] vector to matrix transformation
In addition to Sean's reply look at ?dist and other ways of creating distance / similarity matrices for applications like Mantels Test. Package vegan might be particularly useful. HTH, Andy R > x <- rnorm(10) R > y <- dist(x) R > str(x) num [1:10] -0.431 0.564 0.901 -1.407 -0.991 ... R > str(y) Class 'dist' atomic [1:45] 0.995 1.332 0.977 0.560 0.909 ... ..- attr(*, "Size")= int 10 ..- attr(*, "Diag")= logi FALSE ..- attr(*, "Upper")= logi FALSE ..- attr(*, "method")= chr "euclidean" ..- attr(*, "call")= language dist(x = x) R > class(y) [1] "dist" R > class(x) [1] "numeric" R > > -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] Behalf Of Gwenael Jacob > Sent: Friday, December 03, 2004 5:40 AM > To: [EMAIL PROTECTED] > Subject: [R] vector to matrix transformation > > > Dear, > > Some analysis (linear regression) can only be > done from a vectorized dataset whereas others > require a matrix (Mantel tests). I use the two > analyses and thus need to format my data in > matrix and vector. I spent some time trying to > solve the problem and I just gave up. Did anyone > knows how to transform a matrix into a vector and > back-transform a vector into a matrix? > > Thanks by advance, > Gwenaël Jacob > -- > > - > Gwenaël JACOB > Division Biodiversity > Swiss Federal Research Institute WSL > Zuercherstrasse 111/Postfach > CH-8903 Birmensdorf > > SWITZERLAND > > Phone : ++41 1 7392 504 > Fax: ++41 1 7392 215 > > __ > [EMAIL PROTECTED] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] vector to matrix transformation
On 3 Dec 2004 at 11:39, Gwenael Jacob wrote: > Dear, > > Some analysis (linear regression) can only be > done from a vectorized dataset whereas others > require a matrix (Mantel tests). I use the two > analyses and thus need to format my data in > matrix and vector. I spent some time trying to > solve the problem and I just gave up. Did anyone Hi Gwenael You probably gave up too early did you read ?as.matrix, ?as.vector > x<-matrix(1:6,3,2) > x [,1] [,2] [1,]14 [2,]25 [3,]36 > as.vector(x) [1] 1 2 3 4 5 6 > y<-as.vector(x) > z<-matrix(y,3,2) > z [,1] [,2] [1,]14 [2,]25 [3,]36 Cheers Petr > knows how to transform a matrix into a vector and > back-transform a vector into a matrix? > > Thanks by advance, > Gwenaël Jacob > -- > > - > Gwenaël JACOB > Division Biodiversity > Swiss Federal Research Institute WSL > Zuercherstrasse 111/Postfach > CH-8903 Birmensdorf > > SWITZERLAND > > Phone : ++41 1 7392 504 > Fax: ++41 1 7392 215 > > __ > [EMAIL PROTECTED] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html Petr Pikal [EMAIL PROTECTED] __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] vector to matrix transformation
On Dec 3, 2004, at 5:39 AM, Gwenael Jacob wrote: Dear, Some analysis (linear regression) can only be done from a vectorized dataset whereas others require a matrix (Mantel tests). I use the two analyses and thus need to format my data in matrix and vector. I spent some time trying to solve the problem and I just gave up. Did anyone knows how to transform a matrix into a vector and back-transform a vector into a matrix? See ?as.vector and ?matrix. Do those answer your question? If not, perhaps you could give a bit more detail of what you have tried. Sean __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html