Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine sub lists of different lists into a list of dataframes

Tags:

list

dataframe

r

I have a large number of lists which each consist of many sub lists. Each list contains a sub-list, where each sub-list will have with the same name and number of observations in different lists.

Here's a simple example of data:

score <- list(Bob = list(c('1'), ('0')), Jane = list(c('1'), ('2'), ('4'), ('2')))
comments <- list(Bob = list(c('AAA'), ('BBB')), Jane = list(c('ZZZ'), ('XXX'), ('YYY'), ('QQQ')))

I am looking to create a list of data frames that combines the sub-lists together and preserves the names.

my.list.Bob
score   comments  
1    AAA  
0    BBB

my.list.Jane  
score   comments  
1    ZZZ  
2    XXX  
4    YYY  
2    QQQ  
like image 513
MAXprogress Avatar asked Apr 06 '18 02:04

MAXprogress


People also ask

How do I merge lists into data frames?

concat() to merge a list of DataFrames into a single DataFrame. Call pandas. concat(df_list) with df_list as a list of pandas. DataFrame s with the same column labels to merge the DataFrame s into a single DataFrame .

How do I combine a list of DataFrames in R?

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

How do you combine lists in python?

In python, we can use the + operator to merge the contents of two lists into a new list. For example, We can use + operator to merge two lists i.e. It returned a new concatenated lists, which contains the contents of both list_1 and list_2.


2 Answers

A solution using tidyverse. The idea is to use map2 loop through each element from two lists, use map_dfr and as_data_frame to create data frame, and then use bind_cols to combine each data frame.

library(tidyverse)

map2(score, comments, function(x, y){
  X <- map_dfr(x, as_data_frame)
  Y <- map_dfr(y, as_data_frame)
  dat <- bind_cols(X, Y) %>%
    set_names(c("score", "comments"))
})
# $Bob
# # A tibble: 2 x 2
#   score comments
#   <chr> <chr>   
# 1 1     AAA     
# 2 0     BBB     
# 
# $Jane
# # A tibble: 4 x 2
#   score comments
#   <chr> <chr>   
# 1 1     ZZZ     
# 2 2     XXX     
# 3 4     YYY     
# 4 2     QQQ
like image 26
www Avatar answered Sep 28 '22 00:09

www


Here is one way for you. If you have all lists in your global environment, you can do the following. First, you create a list with all lists you have. Then, you use transpose() that allows you to create a list for each person (e.g., a list for Bob with score and comments). In each list, you have comments and score as nested lists in this case. You want to unlist them in each list. For that reason, you can use rapply2() by rawr. Finally, you create a data frame for each list.

library(magrittr)
library(purrr)
library(rawr) #devtools::install_github('raredd/rawr')

score <- list(Bob = list(c('1'), ('0')), Jane = list(c('1'), ('2'), ('4'), ('2')))
comments <- list(Bob = list(c('AAA'), ('BBB')), Jane = list(c('ZZZ'), ('XXX'), ('YYY'), ('QQQ')))

# Get all objects in the global environment and create a list.
mylist <- mget(ls(pattern =  ".*"))

purrr::transpose(mylist) %>%
rapply2(unlist, classes = "list") %>%
lapply(as.data.frame, stringsAsFactors = FALSE)

$Bob
  comments score
1      AAA     1
2      BBB     0

$Jane
  comments score
1      ZZZ     1
2      XXX     2
3      YYY     4
4      QQQ     2
like image 184
jazzurro Avatar answered Sep 28 '22 01:09

jazzurro