Leigh E. Lommen
2010-Sep-16 15:18 UTC
[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.dfX1.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]]
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.dfX1.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.
Hi, Well, it works for me: x <- matrix(1:20, nrow=5, ncol=4) data.frame(x[1:5,2:4]) X1 X2 X3 1 6 11 16 2 7 12 17 3 8 13 18 4 9 14 19 5 10 15 20 Maybe with as.data.frame(), or set the drop argument to FALSE: data.frame(x[1:5,2:4,drop=FALSE]) Not sure why it doesn't work for you. Check the output of str(stocks1) HTH, Ivan Le 9/16/2010 17:18, Leigh E. Lommen a écrit :> 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@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. >-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. Säugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra@uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php [[alternative HTML version deleted]]