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

Michael Dewey li@t@ @ending from dewey@myzen@co@uk
Sun Dec 9 16:11:52 CET 2018


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 using 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



More information about the R-help mailing list