R List of lists to dataframe with list name as extra column

This might work

library(purrr)
ans <- map_df(past_earnings_lists, ~as.data.frame(.x), .id="id")

It uses map_df, which will map over lists and convert the result to data frames (if possible). Use the .id argument to add names to each data frame as a column.


as @dshkol commented, the easiest is to use dplyr::bind_rows:

d = data.frame(letter = LETTERS, number = 1:26)
d.list = list(d1 = d, d2 = d)
d.all = dplyr::bind_rows(d.list, .id = "variable")

You can also do this in base R with rbind and do.call:

d.all = do.call(rbind, d.list)

However, this will not give you a column containing the list names. You could parse it from the row.names though:

d.all["variable"] = unlist(lapply(
  strsplit(row.names(d.all), ".", fixed = TRUE), function(x) x[[1]])
)

Alternatively, loop through your data frames frames and add the label manually prior to binding:

for (n in names(d.list))
  d.list[[n]]['name'] = n
d.all = do.call(rbind, d.list)

However, it looks like your data frames don't have column names. I think you'll need to fix that for either solution to work.


@mikeck was in right track. Splitting string using . is tricky as . regex matches any character. So we need escape character \ before the .. For anyone who wants to accomplish this with base R, you may try this:

df <- do.call(rbind, list)
df$listname <- lapply(strsplit(row.names(df), "\\."), '[[', 1)

Tags:

R

Reshape2