Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

reshape2

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.

structure

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
like image 340
Bryanzpope Avatar asked Sep 19 '17 21:09

Bryanzpope


People also ask

Can you include an R list as a column of a data frame?

Now to add a list as a column, create a list with required values. Then, use the name of the data frame and the new column separated by $ and assign this to the list so created. This will assign the list to the column name given and then add it to the dataframe.

How do I combine lists into a Dataframe in R?

To combine data frames stored in a list in R, we can use full_join function of dplyr package inside Reduce function.

Can a list contain a list in R?

The list data structure in R allows the user to store homogeneous (of the same type) or heterogeneous (of different types) R objects. Therefore, a list can contain objects of any type including lists themselves.

How do I add column names to a Dataframe in R?

You can add new columns to a dataframe using the $ and assignment <- operators. To do this, just use the df$name notation and assign a new vector of data to it. As you can see, survey has a new column with the name sex with the values we specified earlier.


2 Answers

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.

like image 59
CPak Avatar answered Sep 18 '22 15:09

CPak


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.

like image 25
mikeck Avatar answered Sep 20 '22 15:09

mikeck