Re: [R] how to keep colnames of matrix when put it into a data frame

2018-12-09 Thread Rui Barradas
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, Micha

Re: [R] how to keep colnames of matrix when put it into a data frame

2018-12-09 Thread Jeff Newmiller
Read ?data.frame In particular, notice the check.names argument. On December 9, 2018 7:11:52 AM PST, Michael Dewey 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

Re: [R] how to keep colnames of matrix when put it into a data frame

2018-12-09 Thread Bert Gunter
Your names are not syntactically valid. Consider: > mat <- matrix(1:9, nrow = 3) > colnames(mat) <- letters[1:3] > mat a 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

Re: [R] how to keep colnames of matrix when put it into a data frame

2018-12-09 Thread Michael Dewey
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 = "

[R] how to keep colnames of matrix when put it into a data frame

2018-12-09 Thread Jinsong Zhao
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.