[R] Data frame from list of lists

Liaw, Andy andy_liaw at merck.com
Mon Sep 22 14:44:19 CEST 2003


Don't know if this will be any faster, and it doesn't give you a data frame,
but the final conversion to data frame is probably fairly easy:

> xx <- do.call("rbind", lapply(ListOfLists, function(x) do.call("cbind",
x)))
> xx
     A   L   T           
[1,] "1" "a" "1064233098"
[2,] "2" "b" "1064233098"
[3,] "3" "c" "1064233098"
[4,] "4" "d" "1064233098"

This gives you a character matrix.  The tricky part (for me) is how to get
that last column back to POSIXct.  I have not dealt with date/time in R
before.

HTH,
Andy


> -----Original Message-----
> From: Gregory Jefferis [mailto:jefferis at stanford.edu] 
> Sent: Monday, September 22, 2003 5:15 AM
> To: r-help at stat.math.ethz.ch
> Subject: [R] Data frame from list of lists
> 
> 
> This seems to be a simple problem, and I feel that there 
> ought to be a simple answer, but I can't seem to find it.
> 
> I have a function that returns a number of values as a 
> heterogeneous list - always the same length and same names(), 
> but a number of different data types, including character.  I 
> want to apply it to many inputs, resulting in a list of lists.
> 
> I would like to turn this list of lists into a single data 
> frame in which each row corresponds to one of the original sublists.
> 
> Here is a toy example:
> 
> myfunc=function(x) return(list(A=x,L=letters[x],T=Sys.time()))
> ListOfLists=lapply(1:4,myfunc)
> ListOfDataFrames=lapply(ListOfLists,as.data.frame)
> df=do.call("rbind",ListOfDataFrames)
> 
> df
> 
> Which gives:
> 
>    A L                   T
> 1  1 a 2003-09-22 02:08:44
> 11 2 b 2003-09-22 02:08:44
> 12 3 c 2003-09-22 02:08:44
> 13 4 d 2003-09-22 02:08:44
> 
> Which is what I want (bar the rownames).  The problem is that 
> this can be very slow, particularly the last rbind step, when 
> I have a large data set (e.g. 5000 rows x20 cols).
> 
> I thought that one improvement might be to preassign the data 
> frame since I know how big it should be and then make 
> assignments row by row.  But it turns out that I can't then 
> assign rows to the data frame one at a time - I get errors 
> because factor levels don't exist e.g.:
> 
> df[5:10,]=df[4,] 
> for (i in 5:10){   
>     df[i,]=as.data.frame(myfunc(i))
> }
> 
> I presume that rbind.data.frame normally looks after adding 
> extra levels to factors as they appear in the new rows being 
> appended to the data frame. If anyone has a solution that is 
> quick (and/or elegant), I would be extremely grateful,
> 
> Greg Jefferis.
> 
> ______________________________________________________________
> ____________
> Greg Jefferis,                          Lab Address: Liqun 
> Luo, Herrin 144
> Neurosciences PhD Programme &                e-mail: 
> jefferis at stanford.edu
> Dept Biological Sciences,                       Lab: (650) 725 5809
> Gilbert Biology Building,                       Fax: (650) 723 0589
> 371 Serra Mall,
> Stanford, CA 94305-5020.                       Home: (650) 326 9597
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list 
> https://www.stat.math.ethz.ch/mailman/listinfo> /r-help
>




More information about the R-help mailing list