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
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
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 anyoneHi Gwenael You probably gave up too early did you read ?as.matrix, ?as.vector> x<-matrix(1:6,3,2) > x[,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6> as.vector(x)[1] 1 2 3 4 5 6> y<-as.vector(x)> z<-matrix(y,3,2) > z[,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 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 > > ______________________________________________ > 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.htmlPetr Pikal petr.pikal at precheza.cz
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: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Gwenael Jacob > Sent: Friday, December 03, 2004 5:40 AM > To: R-help at stat.math.ethz.ch > 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 > > ______________________________________________ > 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 >
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 <gwenael.jacob at wsl.ch> To: <R-help at stat.math.ethz.ch> 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