【问题标题】:join lists together by expanding one list to the length of the joining list通过将一个列表扩展到加入列表的长度来将列表连接在一起
【发布时间】:2020-03-24 03:27:10
【问题描述】:

我有两个想要合并的列表。

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

第二个列表如下:

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

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

编辑:

我意识到我忘记了数据。

数据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)

【问题讨论】:

    标签: r list


    【解决方案1】:

    我们可以使用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)
    

    【讨论】:

    • 谢谢,这行得通。如果我有更多想要作为列的列表怎么办? (我添加了一些数据)。我可以使用pmap 这样做并使用mutate(mean = .y, sd = .z, min = .j) 吗?
    猜你喜欢
    • 2019-08-14
    • 2021-12-28
    • 1970-01-01
    • 2018-01-13
    • 2011-03-04
    • 2010-12-04
    • 2023-03-19
    • 2017-03-05
    • 1970-01-01
    相关资源
    最近更新 更多