[R] question about converting a matrix to a dataframe

Michael Green mig at data-intelligence.dk
Thu Sep 16 17:27:51 CEST 2010


Use as.data.frame instead. It does what you want it to do.

newdata.df<-as.data.frame(stocks1[1:5,2:5])

Cheers,
Michael

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Leigh E. Lommen
Sent: 16. september 2010 17:18
To: r-help at r-project.org
Subject: [R] question about converting a matrix to a dataframe

First I have a matrix called stocks1:

 

> class(stocks1)

[1] "matrix"

 

Here are the first 5 rows of the last 4 columns:

> stocks1[1:5,2:5]

      [,1]  [,2]  [,3]  [,4] 

[1,] 80.73 31.95 25.4  25.69

[2,] 83.66 31.95 27.12 25.2 

[3,] 83.27 32.93 28.74 26.29

[4,] 83.9  34.07 29.77 26.6 

[5,] 82.74 35.18 30.24 27.41

 

Now, why can't I convert this into a dataframe?  It automatically
converts the columns into 1 long row??

> newdata.df<-data.frame(stocks1[1:5,2:5])

> newdata.df

   X1.1  X1.2  X1.3 X1.4  X1.5  X1.6  X1.7  X1.8  X1.9 X1.10 X1.11 X1.12


1 80.73 83.66 83.27 83.9 82.74 31.95 31.95 32.93 34.07 35.18  25.4 27.12

 

  X1.13 X1.14 X1.15 X1.16 X1.17 X1.18 X1.19 X1.20 

1 28.74 29.77 30.24 25.69  25.2 26.29  26.6 27.41

> 

 

Regards,

Leigh


	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
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.



More information about the R-help mailing list