简体   繁体   English

将列表中的多个data.tables组合成两个data.tables的列表

[英]Combining multiple data.tables within a list into a list of two data.tables

I have a function (too lengthy to paste here) which will output a list containing two elements which are both data.tables - let's call them AA and BB. 我有一个函数(太长了,不能粘贴),它将输出一个包含两个元素的列表,这两个元素都是data.tables - 让我们称它们为AA和BB。 Each individual data.table will always have the same number of columns for the function calls, but the row numbers may differ. 每个data.table将始终具有相同的函数调用列数,但行号可能不同。 AA and BB do not have matching column names. AA和BB没有匹配的列名。

This function will be called multiple times and I want to combine ('rbind') all of the AA and BB data.tables (separately) from the function calls into two larger data.tables within a list. 这个函数将被多次调用,我想把所有AA和BB data.tables(单独)从函数调用('rbind')组合成一个列表中的两个更大的data.tables。

To show what I mean, I've created two lists (A and B) which each contain two data.tables (AA and BB). 为了表明我的意思,我创建了两个列表(A和B),每个列表包含两个data.tables(AA和BB)。

require(data.table)

A_1 <- data.table(A = 1:2,B = 2:3)
A_2 <- data.table(C = 100:102,D = 300:302)
A <- list(AA = A_1,BB = A_2)

B_1 <- data.table(A = 2:4,B = 1:3)
B_2 <- data.table(C = 10:12,D = 20:22)
B <- list(AA = B_1,BB = B_2)

Return_list <- function(Name){

  return(get(Name))
}      

Now create the object "List" which is a combination of lists A and B 现在创建对象“List”,它是列表A和B的组合

List <- lapply(c("A","B"),Return_list)

My intended output would be given by the following (in the case where I only called the function twice): 我的预期输出将由以下内容给出(在我只调用该函数两次的情况下):

List_output <- list(AA = rbind(A_1,B_1),BB = rbind(A_2,B_2))

I have looked at a lot of examples on SO where data.tables within a list were combined into one; 我在SO上看了很多例子,列表中的data.tables合二为一; however in this case I want to combine them into two and I can't seem to apply the logic from other examples. 但是在这种情况下,我想将它们组合成两个,我似乎无法应用其他示例中的逻辑。

I've played around with rbindlist, unlist(...,recursive = FALSE) and a whole host of other things but have not come close unfortunately. 我玩过rbindlist,unlist(...,recursive = FALSE)以及其他许多东西,但遗憾的是还没有结束。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks in advance. 提前致谢。

We can use 我们可以用

library(tidyverse)
list(A, B) %>%
   transpose %>% 
   map(bind_rows)

Or with rbindlist from data.table 或者使用rbindlistdata.table

library(data.table)
Map(function(...) rbindlist(list(...)), A, B)

You can use the map2 function from the purrr package to get where you want to be: 您可以使用purrr包中的map2函数来获取您想要的位置:

library(purrr)
List_output <- map2(A, B, rbind)

List_output

$`AA`
   A B
1: 1 2
2: 2 3
3: 2 1
4: 3 2
5: 4 3

$BB
     C   D
1: 100 300
2: 101 301
3: 102 302
4:  10  20
5:  11  21
6:  12  22

Or the Map function from base R: 或者基础R的Map函数:

Map(rbind, A, B)

$`AA`
   A B
1: 1 2
2: 2 3
3: 2 1
4: 3 2
5: 4 3

$BB
     C   D
1: 100 300
2: 101 301
3: 102 302
4:  10  20
5:  11  21
6:  12  22

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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