[R] Can't convert list to matrix properly
Ivan Calandra
ivan.calandra at uni-hamburg.de
Thu Mar 11 17:13:27 CET 2010
Hi!
Try: myMatrix <- as.matrix(myDataFrame)
Looking at ?data.matrix: "Return the matrix obtained by converting all
the variables in a data frame to numeric mode".
You could also do directly: myMatrix <- as.matrix(myList)
Though you might need to transpose (see ?t)
HTH,
Ivan
Le 3/11/2010 17:03, anna a écrit :
> 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
>
--
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 at uni-hamburg.de
**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
More information about the R-help
mailing list