I have a list that contains multiple data.frames. I want to select every nth data.frame from the list and combine them into a single data.frame which can be written to a csv.
Here is an example of the list structure:
one.title <- data.frame(id = '1a', title = 'first title')
one.author <- data.frame(first_name = c('Susan', 'Alice'),
last_name = c('Smith', 'Johnson') )
second.title <- data.frame(id = '2b', title = 'second_title')
second.author <- data.frame(first_name = c('Sarah', 'Mary'),
last_name = c('Davis', 'Proctor') )
one.list <- list()
one.list[[1]]$title <- one.title
one.list[[1]]$author <- one.author
one.list[[2]]$title <- second.title
one.list[[2]]$author <- second.author
Here's my current solution that produces a single data frame for the 'authors' fields:
build_author_table <- function(result.l){
list_to_df <- function(i){
x <- result.l[[i]]$author
return(x)
}
authors_df_l <-(lapply(1:length(result.l), FUN = list_to_df))
authors_df <- do.call("rbind", lapply(authors_df_l, as.data.frame))
return(authors_df)
}
This produces the output I want:
first_name last_name
1 Susan Smith
2 Alice Johnson
3 Sarah Davis
4 Mary Proctor
But as you can probably imagine, when scaled to thousands of records with much larger text fields in the data.frame, it is painfully slow.
Can anyone suggest a faster, more efficient way to produce the final data.frame?