Misha Misha - 3 months ago 60
R Question

convert from list to dataframe in r

I need to dig out similar dataframes from a list and put them into a dataframe. I´ve created a toy example:

nn<-list()
h<-data.frame(a=c(5,6),j=c(8,1),g=c("d","o"))
rnz<-c("test1","test2")
o<-data.frame(a=c(1,2),j=c(6,4),g=c("r","u"))
rownames(h)<-rnz
rownames(o)<-rnz
i<-1:4
nn$set1<-list(num=i,df=h)
nn$set2<-list(num=i/2,df=o)


Now I´d like to extract the list into the following tidy format

var a j
set1 test1 5 8
set1 test2 6 1
set2 test1 1 6
set2 test2 2 4


But - when I do

df<-lapply(nn,function(x) x$df[,c(1,2)])
df2<-lapply(df,function(x) tibble::rownames_to_column(x,"var"))

df3<-do.call(rbind,lapply(df2,function(c) as.data.frame(c,row.names = NULL)))


I get:

var a j
set1.1 test1 5 8
set1.2 test2 6 1
set2.1 test1 1 6
set2.2 test2 2 4


How can I remove remove the .1,.2 etc in the rowcolumn? Is there a neater way of doing this?

Answer

We can use rbindlist with the idcol argument

library(data.table)
rbindlist(lapply(nn, function(x) transform( x$df[1:2], 
         var = row.names(x$df))), idcol = "name")
#    name a j   var
#1: set1 5 8 test1
#2: set1 6 1 test2
#3: set2 1 6 test1
#4: set2 2 4 test2