首页 > 解决方案 > 通过将一个列表扩展到加入列表的长度来将列表连接在一起

问题描述

我有两个想要加入的列表。

我想将第二个列表作为新列加入第一个列表。

第二个列表如下所示:

[[1]]
[1] 2.46

[[2]]
[1] 2.475

[[3]]
[1] 2.4875

[[4]]
[1] 2.485

[[5]]
[1] 2.4625

[[6]]
[1] 2.4875

因此,我想[[1]]作为列表 1 中的新列加入。其中 2 个列表的预期输出:

    [[1]]
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species Value
1          5.1         3.5          1.4         0.2  setosa 2.46
2          4.9         3.0          1.4         0.2  setosa 2.46
3          4.7         3.2          1.3         0.2  setosa 2.46
4          4.6         3.1          1.5         0.2  setosa 2.46
5          5.0         3.6          1.4         0.2  setosa 2.46
6          5.4         3.9          1.7         0.4  setosa 2.46

[[2]]
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species Value
2          4.9         3.0          1.4         0.2  setosa 2.475
3          4.7         3.2          1.3         0.2  setosa 2.475
4          4.6         3.1          1.5         0.2  setosa 2.475
5          5.0         3.6          1.4         0.2  setosa 2.475
6          5.4         3.9          1.7         0.4  setosa 2.475
7          4.6         3.4          1.4         0.3  setosa 2.475

这与我在这里遇到的一个问题有关,在 EDIT 中我计算的平均值是此处显示的值。

编辑:

我意识到我忘记了数据。

数据1

list1 <- list(structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4
), Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9), Petal.Length = c(1.4, 
1.4, 1.3, 1.5, 1.4, 1.7), Petal.Width = c(0.2, 0.2, 0.2, 0.2, 
0.2, 0.4), Species = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("setosa", 
"versicolor", "virginica"), class = "factor")), row.names = c(NA, 
6L), class = "data.frame"), structure(list(Sepal.Length = c(4.9, 
4.7, 4.6, 5, 5.4, 4.6), Sepal.Width = c(3, 3.2, 3.1, 3.6, 3.9, 
3.4), Petal.Length = c(1.4, 1.3, 1.5, 1.4, 1.7, 1.4), Petal.Width = c(0.2, 
0.2, 0.2, 0.2, 0.4, 0.3), Species = structure(c(1L, 1L, 1L, 1L, 
1L, 1L), .Label = c("setosa", "versicolor", "virginica"), class = "factor")), row.names = 2:7, class = "data.frame"), 
    structure(list(Sepal.Length = c(4.7, 4.6, 5, 5.4, 4.6, 5), 
        Sepal.Width = c(3.2, 3.1, 3.6, 3.9, 3.4, 3.4), Petal.Length = c(1.3, 
        1.5, 1.4, 1.7, 1.4, 1.5), Petal.Width = c(0.2, 0.2, 0.2, 
        0.4, 0.3, 0.2), Species = structure(c(1L, 1L, 1L, 1L, 
        1L, 1L), .Label = c("setosa", "versicolor", "virginica"
        ), class = "factor")), row.names = 3:8, class = "data.frame"), 
    structure(list(Sepal.Length = c(4.6, 5, 5.4, 4.6, 5, 4.4), 
        Sepal.Width = c(3.1, 3.6, 3.9, 3.4, 3.4, 2.9), Petal.Length = c(1.5, 
        1.4, 1.7, 1.4, 1.5, 1.4), Petal.Width = c(0.2, 0.2, 0.4, 
        0.3, 0.2, 0.2), Species = structure(c(1L, 1L, 1L, 1L, 
        1L, 1L), .Label = c("setosa", "versicolor", "virginica"
        ), class = "factor")), row.names = 4:9, class = "data.frame"), 
    structure(list(Sepal.Length = c(5, 5.4, 4.6, 5, 4.4, 4.9), 
        Sepal.Width = c(3.6, 3.9, 3.4, 3.4, 2.9, 3.1), Petal.Length = c(1.4, 
        1.7, 1.4, 1.5, 1.4, 1.5), Petal.Width = c(0.2, 0.4, 0.3, 
        0.2, 0.2, 0.1), Species = structure(c(1L, 1L, 1L, 1L, 
        1L, 1L), .Label = c("setosa", "versicolor", "virginica"
        ), class = "factor")), row.names = 5:10, class = "data.frame"), 
    structure(list(Sepal.Length = c(5.4, 4.6, 5, 4.4, 4.9, 5.4
    ), Sepal.Width = c(3.9, 3.4, 3.4, 2.9, 3.1, 3.7), Petal.Length = c(1.7, 
    1.4, 1.5, 1.4, 1.5, 1.5), Petal.Width = c(0.4, 0.3, 0.2, 
    0.2, 0.1, 0.2), Species = structure(c(1L, 1L, 1L, 1L, 1L, 
    1L), .Label = c("setosa", "versicolor", "virginica"), class = "factor")), row.names = 6:11, class = "data.frame"))

数据2:

list2 <- list(2.46, 2.475, 2.4875, 2.485, 2.4625, 2.4875)

数据3:

list3 <- list(1.80438213020271, 1.81796589626978, 1.81591080488058, 1.81906569425076, 
    1.81978971735325, 1.86302586794048)

数据4:

list4 <- list(0.1, 0.1, 0.1, 0.1, 0.1, 0.1)

标签: rlist

解决方案


我们可以用map2

library(dplyr)
library(purrr)
map2(lst1, lst2, ~ .x %>%
                       mutate(Value = .y))

如果有更多列表,我们可以将其包装在一个列表中list并使用pmap

pmap(list(lst1, lst2, lst3, lst4), ~ ..1 %>%
                       mutate(mean = ..2, sd = ..3,  min = ..4))

base RMap

Map(cbind, lst1, Value = lst2)

推荐阅读