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

眉间皱痕 提交于 2020-01-24 05:18:05

问题


I have a list of lists that have names.

I want to add them all together into a dataframe but keep all the columns

past_earnings_lists[1]

successfully returns one list from the list of lists

names(past_earnings_lists)[1]

successfully returns the name of the list

past_earnings <- melt(past_earnings_lists)

puts all the data in one data frame but doesn't keep structure

past_earnings <- as.data.frame.matrix(past_earnings_lists$ADBE)

successfully takes one list and keeps the structure but doesn't add the name of the list to the dataframe.

for example, adbe has 7 columns and 30 rows, i want it to add an 8th column with the name, adbe, and append it to a dataframe with all the other lists doing the same.

i want a dataframe with the results being
  sym  v1 v2 v3 v4 v5 v6 v7
1 adbe  1  2  3  4  5  6  7
2 adbe  1  2  3  4  5  6  7
3 air   1  2  3  4  5  6  7
4 air   1  2  3  4  5  6  7
5 alog  1  2  3  4  5  6  7
and so on

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/46310288/r-list-of-lists-to-dataframe-with-list-name-as-extra-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!