Cbind Two Lists of Data.Frames to a New List

cbind two lists of data.frames to a new list

You can use Map

Map(cbind, a, b)

combining data frames from two lists

It looks like this is what you want:

map2(x, y, ~ inner_join(.x, .y))
[[1]]
i x z x1 z1
1 1 0.7715183 -0.6933826 -0.3335239 0.5957587
2 2 -0.3824746 -0.7248827 -1.6736241 -1.2248904
3 3 0.3412777 -0.3711940 0.9334678 0.4043867
4 4 -0.4225862 -1.6653314 1.0369985 1.1808140
5 5 0.7468157 0.1704126 -0.1470796 -1.6237296

[[2]]
i x z x1 z1
1 1 0.69264103 -0.6640663 -0.2253319 0.26323254
2 2 -0.07861775 0.7914119 0.3725911 0.02854667
3 3 -0.86588724 -0.5519633 -1.5114177 -0.14283509
4 4 1.16069947 1.1299540 -0.4207173 -1.15829758
5 5 2.13867104 -0.9668079 0.1082068 -2.74714297

Combine two lists of dataframes, dataframe by dataframe

> mapply(cbind, opr, timestamp, SIMPLIFY=FALSE)
$Cycling1.opr
V1 V2 timestamp
1 1 21 1
2 2 22 2
3 3 23 3
4 4 24 4
5 5 25 5

$Cycling2.opr
V1 V2 timestamp
1 1 21 1
2 2 22 2
3 3 23 3
4 4 24 4
5 5 25 5
6 6 26 6
7 7 27 7
8 8 28 8
9 9 29 9
10 10 30 10

$Cycling3.opr
V1 V2 timestamp
1 1 21 1
2 2 22 2
3 3 23 3
4 4 24 4
5 5 25 5
6 6 26 6
7 7 27 7
8 8 28 8
9 9 29 9
10 10 30 10
11 11 31 11
12 12 32 12
13 13 33 13
14 14 34 14
15 15 35 15
16 16 36 16
17 17 37 17
18 18 38 18
19 19 39 19
20 20 40 20
21 21 41 21
22 22 42 22
23 23 43 23
24 24 44 24
25 25 45 25
26 26 46 26
27 27 47 27
28 28 48 28
29 29 49 29
30 30 50 30

Here's the structure:

> str(mapply(cbind, opr, timestamp, SIMPLIFY=FALSE))
List of 3
$ Cycling1.opr:'data.frame': 5 obs. of 3 variables:
..$ V1 : int [1:5] 1 2 3 4 5
..$ V2 : int [1:5] 21 22 23 24 25
..$ timestamp: int [1:5] 1 2 3 4 5
$ Cycling2.opr:'data.frame': 10 obs. of 3 variables:
..$ V1 : int [1:10] 1 2 3 4 5 6 7 8 9 10
..$ V2 : int [1:10] 21 22 23 24 25 26 27 28 29 30
..$ timestamp: int [1:10] 1 2 3 4 5 6 7 8 9 10
$ Cycling3.opr:'data.frame': 30 obs. of 3 variables:
..$ V1 : int [1:30] 1 2 3 4 5 6 7 8 9 10 ...
..$ V2 : int [1:30] 21 22 23 24 25 26 27 28 29 30 ...
..$ timestamp: int [1:30] 1 2 3 4 5 6 7 8 9 10 ...

join two lists of data frames into a single list of binded_rows data frames

We need map2 for binding two corresponding lists

library(purrr)
map2_dfr(rapheys_df_list, XGB_models_Prep, bind_rows)

data

rapheys_df_list <- list(data.frame(col1 = 1:3, col2 = 4:6), 
data.frame(col1 = 7:9, col2 = 10:12))
XGB_models_Prep <- list(data.frame(col1 = 2:5, col2 = 3:6),
data.frame(col1 = 4:6, col2 = 0:2))

R: merge two lists of lists of dataframes

Here is a solution using base R:

x <- c(L1, L2)
lapply(split(x, names(x)), function(i){
xsub <- do.call(c, unname(i))
lapply(split(xsub, names(xsub)), function(j) do.call(rbind, unname(j)))
})
  • split(x, names(x)) will put Q1s together and Q2s together;
  • xsub <- do.call(c, unname(i)) will combine Q1s or Q2s into a list data.frames;
  • split(xsub, names(xsub)) will group data.frames by their names (A, B, C);

The output is:

# $Q1
# $Q1$A
# X1
# 1 1
# 2 2
# 3 3
#
# $Q1$B
# X1
# 1 4
# 2 5
# 3 6
#
# $Q1$C
# X1
# 1 1
# 2 2
# 3 3
# 4 4
# 5 5
# 6 6
#
#
# $Q2
# $Q2$A
# X1
# 1 4
# 2 5
# 3 6
#
# $Q2$B
# X1
# 1 1
# 2 2
# 3 3
#
# $Q2$C
# X1
# 1 1
# 2 2
# 3 3
# 4 4
# 5 5
# 6 6

Cbind elements of a list

Use Map :

list_all <- Map(cbind, list1, list2)

Or map2 in purrr :

list_all <- purrr::map2(list1, list2, cbind)

Combine dataframes in two different lists keyed on the element name in R

You can try using the unique names from list1 and list2 as you have already tried and then use them to setnames:

keys <- unique(c(names(list1), names(list2)))
x <- setNames(Map(rbind, list1[keys], list2[keys]), keys)
identical(x, combined_list)
#[1] TRUE

or using lapply:

x <- lapply(setNames(keys, keys), function(x) {rbind(list1[[x]], list2[[x]])})
identical(x, combined_list)
#[1] TRUE

Combine two lists in a dataframe in R

This is another option:

do.call(rbind, Map(data.frame, A=listA, B=listB))

# A B
# 1 a 0.05
# 2 b 0.05
# 3 c 0.05
# 4 d 0.50
# 5 e 0.50


Related Topics



Leave a reply



Submit