[R] Data frame from list of lists
Gregory Jefferis
jefferis at stanford.edu
Mon Sep 22 11:15:14 CEST 2003
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
More information about the R-help
mailing list