[R] Convert matrix to data.frame

Tony Plate tplate at acm.org
Thu Jan 12 21:27:30 CET 2006


When I try converting a matrix to a data frame, it works for me:

 > x <- matrix(1:6,ncol=2,dimnames=list(LETTERS[1:3],letters[24:25]))
 > data.frame(x)
   x y
A 1 4
B 2 5
C 3 6
 > str(data.frame(x))
`data.frame':   3 obs. of  2 variables:
  $ x: int  1 2 3
  $ y: int  4 5 6
 >

You can also use as.data.frame() to convert a matrix to a data.frame 
(but note that if colnames are missing form the matrix, as.data.frame() 
  constructs different colnames than does data.frame().

You say "it didn't work" -- it's difficult to help with such a 
non-specific complaint.  Can you explain exactly how it didn't work for 
you?  (e.g., show the exact error message).

-- Tony Plate

Chia, Yen Lin wrote:
> Hi all,
> 
>  
> 
> I wonder how could I convert a matrix A to a dataframe such that
> whenever I'm running a linear model such lme, I can use A$x1?  I tried
> data.frame(A), it didn't work.  Should I initialize A not as a matrix?
> Thanks.
> 
>  
> 
> Yen Lin
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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
>




More information about the R-help mailing list