Muhammad Subianto
2006-Apr-06 14:16 UTC
[R] convert a data frame to matrix - changed column name
I have a question, which very easy to solve, but I can't find a solution. I want to convert a data frame to matrix. Here my toy example:> L3 <- c(1:3) > L10 <- c(1:6) > d <- data.frame(cbind(x=c(10,20), y=L10), fac=sample(L3, + 6, repl=TRUE)) > dx y fac 1 10 1 1 2 20 2 1 3 10 3 1 4 20 4 3 5 10 5 2 6 20 6 2> is.data.frame(d)[1] TRUE> sapply(d, function(x) unlist(x, use.names=FALSE))x y fac [1,] 10 1 1 [2,] 20 2 1 [3,] 10 3 1 [4,] 20 4 3 [5,] 10 5 2 [6,] 20 6 2> is.matrix(sapply(d, function(x) unlist(x, use.names=FALSE)))[1] TRUE>Yes, I get a matrix TRUE. But I need to change a column name like [,1] [,2] [,3]. I need the result like [,1] [,2] [,3] [1,] 10 1 1 [2,] 20 2 1 [3,] 10 3 1 [4,] 20 4 3 [5,] 10 5 2 [6,] 20 6 2 How can I do that? Thanks in advance, Muhammad Subianto
Robin Hankin
2006-Apr-06 14:22 UTC
[R] convert a data frame to matrix - changed column name
Hi set the column names to NULL: > a <- data.frame(x=1:4,y=4:1) > aa <- as.matrix(a) > colnames(aa) <- NULL > aa [,1] [,2] 1 1 4 2 2 3 3 3 2 4 4 1 best wishes Robin On 6 Apr 2006, at 15:16, Muhammad Subianto wrote:> I have a question, which very easy to solve, but I can't find a > solution. > I want to convert a data frame to matrix. Here my toy example: > >> L3 <- c(1:3) >> L10 <- c(1:6) >> d <- data.frame(cbind(x=c(10,20), y=L10), fac=sample(L3, + 6, >> repl=TRUE)) >> d > x y fac > 1 10 1 1 > 2 20 2 1 > 3 10 3 1 > 4 20 4 3 > 5 10 5 2 > 6 20 6 2 >> is.data.frame(d) > [1] TRUE >> sapply(d, function(x) unlist(x, use.names=FALSE)) > x y fac > [1,] 10 1 1 > [2,] 20 2 1 > [3,] 10 3 1 > [4,] 20 4 3 > [5,] 10 5 2 > [6,] 20 6 2 >> is.matrix(sapply(d, function(x) unlist(x, use.names=FALSE))) > [1] TRUE >> > > Yes, I get a matrix TRUE. But I need to change a column name like [,1] > [,2] [,3]. I need the result like > > [,1] [,2] [,3] > [1,] 10 1 1 > [2,] 20 2 1 > [3,] 10 3 1 > [4,] 20 4 3 > [5,] 10 5 2 > [6,] 20 6 2 > > How can I do that? > Thanks in advance, Muhammad Subianto > > ______________________________________________ > 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-- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743
Dimitris Rizopoulos
2006-Apr-06 14:28 UTC
[R] convert a data frame to matrix - changed column name
try the following: out <- data.matrix(d) dimnames(out) <- NULL out 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://www.med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Muhammad Subianto" <msubianto at gmail.com> To: <r-help at stat.math.ethz.ch> Sent: Thursday, April 06, 2006 4:16 PM Subject: [R] convert a data frame to matrix - changed column name>I have a question, which very easy to solve, but I can't find a >solution. > I want to convert a data frame to matrix. Here my toy example: > >> L3 <- c(1:3) >> L10 <- c(1:6) >> d <- data.frame(cbind(x=c(10,20), y=L10), fac=sample(L3, + 6, >> repl=TRUE)) >> d > x y fac > 1 10 1 1 > 2 20 2 1 > 3 10 3 1 > 4 20 4 3 > 5 10 5 2 > 6 20 6 2 >> is.data.frame(d) > [1] TRUE >> sapply(d, function(x) unlist(x, use.names=FALSE)) > x y fac > [1,] 10 1 1 > [2,] 20 2 1 > [3,] 10 3 1 > [4,] 20 4 3 > [5,] 10 5 2 > [6,] 20 6 2 >> is.matrix(sapply(d, function(x) unlist(x, use.names=FALSE))) > [1] TRUE >> > > Yes, I get a matrix TRUE. But I need to change a column name like > [,1] > [,2] [,3]. I need the result like > > [,1] [,2] [,3] > [1,] 10 1 1 > [2,] 20 2 1 > [3,] 10 3 1 > [4,] 20 4 3 > [5,] 10 5 2 > [6,] 20 6 2 > > How can I do that? > Thanks in advance, Muhammad Subianto > > ______________________________________________ > 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 >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Muhammad Subianto
2006-Apr-06 14:33 UTC
[R] convert a data frame to matrix - changed column name
On this day 06/04/2006 16:22, Robin Hankin wrote:> Hi > > set the column names to NULL: > > > > a <- data.frame(x=1:4,y=4:1) > > aa <- as.matrix(a) > > colnames(aa) <- NULL > > aaOn this day 06/04/2006 16:28, Dimitris Rizopoulos wrote:> try the following: > > out <- data.matrix(d) > dimnames(out) <- NULL > outThank you very much for your help. Best, Muhammad Subianto On 4/6/06, Muhammad Subianto <msubianto at gmail.com> wrote:> I have a question, which very easy to solve, but I can't find a solution. > I want to convert a data frame to matrix. Here my toy example: > > > L3 <- c(1:3) > > L10 <- c(1:6) > > d <- data.frame(cbind(x=c(10,20), y=L10), fac=sample(L3, + 6, repl=TRUE)) > > d > x y fac > 1 10 1 1 > 2 20 2 1 > 3 10 3 1 > 4 20 4 3 > 5 10 5 2 > 6 20 6 2 > > is.data.frame(d) > [1] TRUE > > sapply(d, function(x) unlist(x, use.names=FALSE)) > x y fac > [1,] 10 1 1 > [2,] 20 2 1 > [3,] 10 3 1 > [4,] 20 4 3 > [5,] 10 5 2 > [6,] 20 6 2 > > is.matrix(sapply(d, function(x) unlist(x, use.names=FALSE))) > [1] TRUE > > > > Yes, I get a matrix TRUE. But I need to change a column name like [,1] > [,2] [,3]. I need the result like > > [,1] [,2] [,3] > [1,] 10 1 1 > [2,] 20 2 1 > [3,] 10 3 1 > [4,] 20 4 3 > [5,] 10 5 2 > [6,] 20 6 2 > > How can I do that? > Thanks in advance, Muhammad Subianto >