Skip to content
Prev 374401 / 398513 Next

Converting a list to a data frame

On Wed, 2 May 2018, Kevin E. Thorpe wrote:

            
Hi, Kevin.

Here's code that will generate your desired data frame.

# List as provided
thelist <- list(A=data.frame(x=1:2, y=3:4),B=data.frame(x=5:6,y=7:8))
thelist

# Creating the type-vector
type <- c()
for(i in 1:length(thelist)){
   type <- c(type, rep(names(thelist)[i], sapply(thelist, nrow)[i]))
}

# Creating the data frame
df <- data.frame(type, do.call(rbind.data.frame, c(thelist, make.row.names 
= FALSE)))
df


Kind regards,
Eivind K. Dovik
Bergen, NO