Re: [R] convert a data.frame to matrix
On May 17, 2013, at 01:59 , David Winsemius wrote: On May 16, 2013, at 1:46 PM, Hermann Norpois wrote: Hello, I fail to tranfer data from a dataframe to a matrix. jam is from a dataframe (and belongs still to the class dataframe) and should look like m (see below). jam vec1 vec3 d1 d2 1 172 173 223 356 dput (jam) structure(list(vec1 = 172L, vec3 = 173L, d1 = 223L, d2 = 356L), .Names = c(vec1, vec3, d1, d2), row.names = 1L, class = data.frame) m #THIS IS THE AIM [,1] [,2] [1,] 172 223 [2,] 173 356 dput (m) structure(c(172, 173, 223, 356), .Dim = c(2L, 2L)) How can I transform jam to m? jam - structure(list(vec1 = 172L, vec3 = 173L, d1 = 223L, d2 = 356L), .Names = c(vec1, vec3, d1, d2), row.names = 1L, class = data.frame) jm - data.matrix(jam) dim(jm) - c(2,2) # re-dimension a matrix with column-major order jm [,1] [,2] [1,] 172 223 [2,] 173 356 also matrix(unlist(jam),2) [,1] [,2] [1,] 172 223 [2,] 173 356 -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd@cbs.dk Priv: pda...@gmail.com __ R-help@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.
Re: [R] convert a data.frame to matrix
Why not simply: matrix(jam, ncol=2) [,1] [,2] [1,] 172 223 [2,] 173 356 On Thu, May 16, 2013 at 4:46 PM, Hermann Norpois hnorp...@gmail.com wrote: Hello, I fail to tranfer data from a dataframe to a matrix. jam is from a dataframe (and belongs still to the class dataframe) and should look like m (see below). jam vec1 vec3 d1 d2 1 172 173 223 356 dput (jam) structure(list(vec1 = 172L, vec3 = 173L, d1 = 223L, d2 = 356L), .Names = c(vec1, vec3, d1, d2), row.names = 1L, class = data.frame) m #THIS IS THE AIM [,1] [,2] [1,] 172 223 [2,] 173 356 dput (m) structure(c(172, 173, 223, 356), .Dim = c(2L, 2L)) How can I transform jam to m? Thanks Hermann -- Sarah Goslee http://www.functionaldiversity.org __ R-help@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.
Re: [R] convert a data.frame to matrix
On May 16, 2013, at 1:46 PM, Hermann Norpois wrote: Hello, I fail to tranfer data from a dataframe to a matrix. jam is from a dataframe (and belongs still to the class dataframe) and should look like m (see below). jam vec1 vec3 d1 d2 1 172 173 223 356 dput (jam) structure(list(vec1 = 172L, vec3 = 173L, d1 = 223L, d2 = 356L), .Names = c(vec1, vec3, d1, d2), row.names = 1L, class = data.frame) m #THIS IS THE AIM [,1] [,2] [1,] 172 223 [2,] 173 356 dput (m) structure(c(172, 173, 223, 356), .Dim = c(2L, 2L)) How can I transform jam to m? jam - structure(list(vec1 = 172L, vec3 = 173L, d1 = 223L, d2 = 356L), .Names = c(vec1, vec3, d1, d2), row.names = 1L, class = data.frame) jm - data.matrix(jam) dim(jm) - c(2,2) # re-dimension a matrix with column-major order jm [,1] [,2] [1,] 172 223 [2,] 173 356 Thanks Hermann [[alternative HTML version deleted]] __ R-help@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. David Winsemius Alameda, CA, USA __ R-help@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.