Jinsong Zhao
2018-Dec-09 15:05 UTC
[R] how to keep colnames of matrix when put it into a data frame
Hi there, In the following mini-example, I hope to keep the column names of mat, but failed. # mini-example> mat <- matrix(1:9, nrow = 3) > colnames(mat) <- paste("(", 1:3, ")", sep = "") > mat(1) (2) (3) [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9> data.frame(x = 1:3, mat)x X.1. X.2. X.3. 1 1 1 4 7 2 2 2 5 8 3 3 3 6 9 Any hints will be really appreciated. Best, Jinsong [[alternative HTML version deleted]]
Michael Dewey
2018-Dec-09 15:11 UTC
[R] how to keep colnames of matrix when put it into a data frame
Dear Jinsong Try cbind(x = 1:3, mat) and see if that helps Michael On 09/12/2018 15:05, Jinsong Zhao wrote:> Hi there, > > In the following mini-example, I hope to keep the column names of mat, but failed. > > # mini-example >> mat <- matrix(1:9, nrow = 3) >> colnames(mat) <- paste("(", 1:3, ")", sep = "") >> mat > (1) (2) (3) > [1,] 1 4 7 > [2,] 2 5 8 > [3,] 3 6 9 >> data.frame(x = 1:3, mat) > x X.1. X.2. X.3. > 1 1 1 4 7 > 2 2 2 5 8 > 3 3 3 6 9 > > Any hints will be really appreciated. > > Best, > Jinsong > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >-- Michael http://www.dewey.myzen.co.uk/home.html
Bert Gunter
2018-Dec-09 15:29 UTC
[R] how to keep colnames of matrix when put it into a data frame
Your names are not syntactically valid. Consider:> mat <- matrix(1:9, nrow = 3) > colnames(mat) <- letters[1:3] > mata b c [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9> data.frame(x=1:3,mat)x a b c 1 1 1 4 7 2 2 2 5 8 3 3 3 6 9 See ?make.names, and the "Value" section of ?data.frame for how names are constructed. Michael's suggestion produces a matrix, not a data frame. dimnames of matrices apparently have different rules for validity. Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sun, Dec 9, 2018 at 7:05 AM Jinsong Zhao <jszhao at yeah.net> wrote:> Hi there, > > In the following mini-example, I hope to keep the column names of mat, but > failed. > > # mini-example > > mat <- matrix(1:9, nrow = 3) > > colnames(mat) <- paste("(", 1:3, ")", sep = "") > > mat > (1) (2) (3) > [1,] 1 4 7 > [2,] 2 5 8 > [3,] 3 6 9 > > data.frame(x = 1:3, mat) > x X.1. X.2. X.3. > 1 1 1 4 7 > 2 2 2 5 8 > 3 3 3 6 9 > > Any hints will be really appreciated. > > Best, > Jinsong > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
Jeff Newmiller
2018-Dec-09 15:31 UTC
[R] how to keep colnames of matrix when put it into a data frame
Read ?data.frame In particular, notice the check.names argument. On December 9, 2018 7:11:52 AM PST, Michael Dewey <lists at dewey.myzen.co.uk> wrote:>Dear Jinsong > >Try cbind(x = 1:3, mat) >and see if that helps > >Michael > >On 09/12/2018 15:05, Jinsong Zhao wrote: >> Hi there, >> >> In the following mini-example, I hope to keep the column names of >mat, but failed. >> >> # mini-example >>> mat <- matrix(1:9, nrow = 3) >>> colnames(mat) <- paste("(", 1:3, ")", sep = "") >>> mat >> (1) (2) (3) >> [1,] 1 4 7 >> [2,] 2 5 8 >> [3,] 3 6 9 >>> data.frame(x = 1:3, mat) >> x X.1. X.2. X.3. >> 1 1 1 4 7 >> 2 2 2 5 8 >> 3 3 3 6 9 >> >> Any hints will be really appreciated. >> >> Best, >> Jinsong >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. >>-- Sent from my phone. Please excuse my brevity.
Rui Barradas
2018-Dec-09 15:56 UTC
[R] how to keep colnames of matrix when put it into a data frame
Hello, cbind alone will return a matrix, cbind.data.frame is probably what the OP wants. class(cbind(x = 1:3, mat)) #[1] "matrix" cbind.data.frame(x = 1:3, mat) # x (1) (2) (3) #1 1 1 4 7 #2 2 2 5 8 #3 3 3 6 9 Hope this helps, Rui Barradas ?s 15:11 de 09/12/2018, Michael Dewey escreveu:> Dear Jinsong > > Try cbind(x = 1:3, mat) > and see if that helps > > Michael > > On 09/12/2018 15:05, Jinsong Zhao wrote: >> Hi there, >> >> In the following mini-example, I hope to keep the column names of mat, >> but failed. >> >> # mini-example >>> mat <- matrix(1:9, nrow = 3) >>> colnames(mat) <- paste("(", 1:3, ")", sep = "") >>> mat >> ????? (1) (2) (3) >> [1,]?? 1?? 4?? 7 >> [2,]?? 2?? 5?? 8 >> [3,]?? 3?? 6?? 9 >>> data.frame(x = 1:3, mat) >> ?? x X.1. X.2. X.3. >> 1 1??? 1??? 4??? 7 >> 2 2??? 2??? 5??? 8 >> 3 3??? 3??? 6??? 9 >> >> Any hints will be really appreciated. >> >> Best, >> Jinsong >> ????[[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. >> >