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:> myDataFrameX.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: > myMatrixX.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.
Hi, Is this what you want? matrix(unlist(myList),nr=1) Miguel On Thu, Mar 11, 2010 at 4:03 PM, anna <lippelanna24@hotmail.com> wrote:> > 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. > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
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