[R] Can't convert list to matrix properly

anna lippelanna24 at hotmail.com
Thu Mar 11 17:03:41 CET 2010


Hi guys, here is a list of names that I have:

MyList:
> myList<-list("A", "B","C","D")
> myList
[[1]]
[1] "A"

[[2]]
[1] "B"

[[3]]
[1] "C"

[[4]]
[1] "D"

I want to turn this list into a matrix of 1 row and 4 columns with those
four components (A, B, C, D) so here is what I do:

myDataFrame <- data.frame(myList) and here is how myDataFrame looks like:
> myDataFrame  
 X.A. X.B. X.C. X.D.
1    A    B    C    D

Until that point everything is ok because if I want to retrieve one element
I make
> myDataFrame[[1]]
[1]  A
Levels: A

But when I try to convert into matrix by doing this:

> myMatrix <- data.matrix(myDataFrame) I get this:
> myMatrix
 X.A. X.B. X.C. X.D.
[1,]    1    1    1    1
He just keeps the names and delete the components...What am I doing wrong?
Thanks in advance!













-----
Anna Lippel
-- 
View this message in context: http://n4.nabble.com/Can-t-convert-list-to-matrix-properly-tp1589187p1589187.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list