Converting a list to a data frame
On 05/02/2018 07:11 PM, Kevin E. Thorpe wrote:
I suspect this is pretty easy, but I'm having trouble figuring it out.
Basically, I have a list of data frames such as the following example:
list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
I would like to turn this into data frame where the list elements are
essentially rbind'ed together and the element name becomes a new
variable. For example, I would like to turn the list above into a data
frame that looks like this:
data.frame(type=c("A","A","B","B"),x=c(1:2,5:6),y=c(3:4,7:8))
Appreciate any pointers.
Hi Kevin, data.table::rbindlist does exactly what you want in a very efficient way: library(data.table) dat <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8)) rbindlist(dat, idcol = "type") Regards, Denes
Kevin