I needed to create a data frame from a specific sublist of a list. I know the data structure in this particular sublist is constant. For a single list I found do.call() would do the trick:
lst<-list(l1="aa", l2="ab", l3="ac")
fun.sublst1<-function(n) {
a<-c("a",1,n)
return(a)
}
lstoflst<-lapply(lst, fun.sublst1)
do.call(rbind,lstoflst) # create a data frame from a list
However, If I have a list with lists and I want to iterate over a specific sublist, I am not able use do.call(rbind,lstoflst$A) to create the data frame.
# section list of list
fun.sublst2<-function(n) {
a<-c("a",1,n)
b<-c("b",2)
return(list(A=a,B=b))
}
lstoflst<-lapply(lst, fun.sublst2)
# result should create a dataframe consisting of all sublists $A
t(cbind(lstoflst$l1$A,lstoflst$l2$A,lstoflst$l3$A))
With clumsy code it would look like that.
dat<-t(as.data.frame(lstoflst[[1]][[1]]))
for(i in 2:length(lstoflst)) {
dat<-rbind(dat,t(lstoflst[[i]][[1]]))
}
Is there an elegant way to do it with the base R? I guess do.call(rbind,lstoflst, ???) with some other parameters will do. I suppose I need to pass the index or an indexing function. Any help?
I searched but I had no luck with my search term. Probably it has been solved already. Hope you can guide me anyway. Thanks
EDIT: changed headline because my examples only produce matrices as a result.
c()
creates an atomic vector, not a list? - joran