代码之家  ›  专栏  ›  技术社区  ›  C. Guff

将列表中的每个数据帧绑定到另一个列表中的各个数据帧[R]

  •  0
  • C. Guff  · 技术社区  · 1 年前

    我有两个由数据帧组成的列表(在示例中,按列表列出3个数据帧)。我想绑定这两个列表的数据帧行。

    我想结婚 rbind() 具有 Map() , map() lapply() :

    Map(rbind,List1,List2)

    然而,当我应用该代码时,我只将第一个列表的一个数据帧绑定到第二个列表中的另一个。在最终输出中只给出三个最终数据帧。而我希望第一个列表的每个数据帧被第二个列表的每一个数据帧绑定。给定9个数据帧的输出,而不是我实际拥有的3个。

    如果我有 清单1 由组成 df-a , df-b , df-c ,和 清单2 由组成 df-1 , df-2 , df-3

    我想在列表中有数据帧的最终组合:

    • a-1,
    • a-2,
    • a-3,
    • b-1,
    • b-2,
    • b-3,
    • c-1,
    • c-2,
    • c-3

    相反,使用上面的代码,我只有:

    • a-1,
    • b-2,
    • c-3

    我该如何解决此问题?

    在下面,您将找到一个列表示例:

    列表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"))
    

    列表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"))
    
    2 回复  |  直到 1 年前
        1
  •  2
  •   Andre Wildberg    1 年前

    看看这是否适合你

    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
    
    ...
    

    它实际上只是一个替身 lapply

    lapply(List1, \(x) lapply(List2, \(y) rbind(x, y)))
    
        2
  •  1
  •   margusl    1 年前

    一种选择是首先使用创建组合 expand.grid() / tidyr::expand_grid() 然后 rbind() 通过 pmap() .

    直接使用框架列表:

    expand.grid(List1, List2) |> 
      purrr::pmap(rbind) |>
      str()
    #> List of 9
    #>  $ :Classes 'tbl_df', 'tbl' and 'data.frame':    20 obs. of  3 variables:
    #>   ..$ Values: num [1:20] 3.95 4.29 4.59 4.87 5.14 ...
    #>   ..$ array : num [1:20] 10 12 14 16 18 20 22 24 26 28 ...
    #>   ..$ ID    : chr [1:20] "1" "1" "1" "1" ...
    
    #>   /../
    
    #>  $ :Classes 'tbl_df', 'tbl' and 'data.frame':    20 obs. of  3 variables:
    #>   ..$ Values: num [1:20] 4.31 4.62 4.9 5.16 5.4 ...
    #>   ..$ array : num [1:20] 10 12 14 16 18 20 22 24 26 28 ...
    #>   ..$ ID    : chr [1:20] "3" "3" "3" "3" ...
    

    或者通过索引:

    expand.grid(idx_1 = seq_along(List1), idx_2 = seq_along(List2)) |> 
      purrr::pmap(\(idx_1, idx_2) rbind(List1[[idx_1]], List2[[idx_2]])) |>
      str()
    #> List of 9
    #>  $ :Classes 'tbl_df', 'tbl' and 'data.frame':    20 obs. of  3 variables:
    #>   ..$ Values: num [1:20] 3.95 4.29 4.59 4.87 5.14 ...
    #>   ..$ array : num [1:20] 10 12 14 16 18 20 22 24 26 28 ...
    #>   ..$ ID    : chr [1:20] "1" "1" "1" "1" ...
    
    #>   /../
    
    #>  $ :Classes 'tbl_df', 'tbl' and 'data.frame':    20 obs. of  3 variables:
    #>   ..$ Values: num [1:20] 4.31 4.62 4.9 5.16 5.4 ...
    #>   ..$ array : num [1:20] 10 12 14 16 18 20 22 24 26 28 ...
    #>   ..$ ID    : chr [1:20] "3" "3" "3" "3" ...
    

    创建于2024-06-09 reprex v2.1.0