简体   繁体   中英

Combine sub lists of different lists into a list of dataframes

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  

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 (eg, 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

A solution using . 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM