I have two lists composed of dataframes (in the example 3 dataframes per list). I would like to bind the rows of dataframes of the two lists.
I was thinking to couple rbind()
with Map()
, map()
or lapply()
: Map(rbind,List1,List2)
However, when I apply that code, I have a binding of only one dataframe of the first list to another one of in the second list. Given only three final dataframes in the final output. While I would like that each dataframe of the first list being binded by each dataframe of the second list. Given a output of 9 dataframe instead of the 3 I have actually.
If I have a List1 composed of df-a, df-b, df-c, and a List2 composed of df-1, df-2, df-3
I would like to have the final combination of dataframe in the list :
instead of that, with the code on above I only have :
List 1
List1 <- structure(list(structure(list(Values = c(3.95464079265625, 4.28848139983694,
4.59263295848293, 4.87348316186162, 5.13543268303855, 5.38166290292844,
5.61456044441555, 5.8359690473681, 6.04734746369445, 6.24987292533157
), array = c(10, 12, 14, 16, 18, 20, 22, 24, 26, 28), ID = c("1",
"1", "1", "1", "1", "1", "1", "1", "1", "1")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -10L)), structure(list(
Values = c(6.82179666846063, 7.09317719563801, 7.33103172027803,
7.54350812736648, 7.73603145714462, 7.91240924925746, 8.07542410682578,
8.22717585659922, 8.36929092723351, 8.50305660066192), array = c(10,
12, 14, 16, 18, 20, 22, 24, 26, 28), ID = c("2", "2", "2",
"2", "2", "2", "2", "2", "2", "2")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -10L)), structure(list(
Values = c(4.30943511800407, 4.62131690026944, 4.90255443339214,
5.15997082435108, 5.39822432778288, 5.62065617021581, 5.8297555323974,
6.02743344284351, 6.21519337899337, 6.39424241723516), array = c(10,
12, 14, 16, 18, 20, 22, 24, 26, 28), ID = c("3", "3", "3",
"3", "3", "3", "3", "3", "3", "3")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -10L))), ptype = structure(list(
Values = numeric(0), array = numeric(0), ID = character(0)), class = c("tbl_df",
"tbl", "data.frame"), row.names = integer(0)), class = c("vctrs_list_of",
"vctrs_vctr", "list"))
List 2
List2 <- structure(list(structure(list(Values = c(8.17331050066691, 8.87216395029884,
9.50945470046081, 10.098386534235, 10.6480633233427, 11.1650711748644,
11.6543539639589, 12.1197331206333, 12.5642336602292, 12.9902979406118
), array = c(10, 12, 14, 16, 18, 20, 22, 24, 26, 28), ID = c("1",
"1", "1", "1", "1", "1", "1", "1", "1", "1")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -10L)), structure(list(
Values = c(7.98726323968068, 8.65441558454706, 9.26177728829764,
9.82224562340532, 10.3446999917905, 10.8355558754145, 11.2996236117639,
11.7406176335271, 12.1614755496734, 12.5645671491611), array = c(10,
12, 14, 16, 18, 20, 22, 24, 26, 28), ID = c("2", "2", "2",
"2", "2", "2", "2", "2", "2", "2")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -10L)), structure(list(
Values = c(7.34557601230256, 8.06135956556851, 8.72069616000395,
9.33527354603708, 9.9132220632988, 10.4604689570926, 10.9814938935561,
11.4797806661262, 11.9581023062164, 12.4187090723963), array = c(10,
12, 14, 16, 18, 20, 22, 24, 26, 28), ID = c("3", "3", "3",
"3", "3", "3", "3", "3", "3", "3")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -10L))), ptype = structure(list(
Values = numeric(0), array = numeric(0), ID = character(0)), class = c("tbl_df",
"tbl", "data.frame"), row.names = integer(0)), class = c("vctrs_list_of",
"vctrs_vctr", "list"))
See if this works for you
Map(\(x) lapply(List2, \(y) rbind(x, y)), List1)
[[1]]
[[1]][[1]]
# A tibble: 20 × 3
Values array ID
<dbl> <dbl> <chr>
1 3.95 10 1
2 4.29 12 1
3 4.59 14 1
4 4.87 16 1
5 5.14 18 1
6 5.38 20 1
7 5.61 22 1
8 5.84 24 1
9 6.05 26 1
10 6.25 28 1
11 8.17 10 1
12 8.87 12 1
13 9.51 14 1
14 10.1 16 1
15 10.6 18 1
16 11.2 20 1
17 11.7 22 1
18 12.1 24 1
19 12.6 26 1
20 13.0 28 1
...
It's effectively just a double lapply
lapply(List1, \(x) lapply(List2, \(y) rbind(x, y)))