Skip to content
Prev 374416 / 398513 Next

Converting a list to a data frame

On 05/03/2018 07:58 AM, Jeff Newmiller wrote:
In response to Jeff's note, this a solution which results in a 
data.frame instead of a data.table:

library(data.table)
dat <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
dat <- rbindlist(dat, idcol = "type")

# traditional way
dat_df <- as.data.frame(dat)

# no copy (assignment not needed, memory-efficient)
setDF(dat)

Further amendments would be needed to transform the 'type' variable to a 
factor, if it is required.