附加两个列表列表的列表
Appending the lists of two lists of lists
我有两个列表如下:
list1 <- list(c(`0` = 0L, `25` = 0L, `100` = 1L, `250` = 1L, `500` = 1L,
`1000` = 1L, Infinity = 3L), c(`0` = 0L, `25` = 0L, `100` = 1L,
`250` = 1L, `500` = 1L, Infinity = 4L))
list2 <- list(c(`0` = 0L, `25` = 0L, `100` = 0L, `250` = 2L, `500` = 1L,
`1000` = 1L, Infinity = 3L), c(`0` = 0L, `25` = 0L, `100` = 1L,
`250` = 1L, `500` = 1L, Infinity = 4L))
我想将 list2[[1]]
附加到 list1[[1]]
并将 list2[[2]]
附加到 list1[[2]]
。这样:
list_out <- list(c(`0` = 0L, `25` = 0L, `100` = 1L, `250` = 1L, `500` = 1L,
`1000` = 1L, Infinity = 3L, `0` = 0L, `25` = 0L, `100` = 0L, `250` = 2L, `500` = 1L,
`1000` = 1L, Infinity = 3L), c(`0` = 0L, `25` = 0L, `100` = 1L,
`250` = 1L, `500` = 1L, Infinity = 4L, `0` = 0L, `25` = 0L, `100` = 1L,
`250` = 1L, `500` = 1L, Infinity = 4L))
谁能帮我解释一下我应该怎么做?
您可以使用 lapply
和 c()
。
lapply(1:length(list1), function(x) c(list1[[x]], list2[[x]]))
或mapply
与append
或c
:
mapply(append, list1, list2)
输出
[[1]]
0 25 100 250 500 1000 Infinity 0
0 0 1 1 1 1 3 0
25 100 250 500 1000 Infinity
0 0 2 1 1 3
[[2]]
0 25 100 250 500 Infinity 0 25
0 0 1 1 1 4 0 0
100 250 500 Infinity
1 1 1 4
检查它是否与您的相同list_out
:
identical(lapply(1:length(list1), function(x) c(list1[[x]], list2[[x]])), list_out)
[1] TRUE
identical(mapply(append, list1, list2), list_out)
[1] TRUE
你可以使用基础函数append
:
> append(list1[[1]], list2[[1]])
0 25 100 250 500 1000 Infinity 0 25 100
0 0 1 1 1 1 3 0 0 0
250 500 1000 Infinity
2 1 1 3
> append(list1[[2]], list2[[2]])
0 25 100 250 500 Infinity 0 25 100 250
0 0 1 1 1 4 0 0 1 1
500 Infinity
1 4
这是另一个基本的 R 解决方案。
Map(c, list1, list2)
identical(Map(c, list1, list2), list_out)
#[1] TRUE
另一种选择是map2
library(purrr)
map2(list1, list2, c)
我有两个列表如下:
list1 <- list(c(`0` = 0L, `25` = 0L, `100` = 1L, `250` = 1L, `500` = 1L,
`1000` = 1L, Infinity = 3L), c(`0` = 0L, `25` = 0L, `100` = 1L,
`250` = 1L, `500` = 1L, Infinity = 4L))
list2 <- list(c(`0` = 0L, `25` = 0L, `100` = 0L, `250` = 2L, `500` = 1L,
`1000` = 1L, Infinity = 3L), c(`0` = 0L, `25` = 0L, `100` = 1L,
`250` = 1L, `500` = 1L, Infinity = 4L))
我想将 list2[[1]]
附加到 list1[[1]]
并将 list2[[2]]
附加到 list1[[2]]
。这样:
list_out <- list(c(`0` = 0L, `25` = 0L, `100` = 1L, `250` = 1L, `500` = 1L,
`1000` = 1L, Infinity = 3L, `0` = 0L, `25` = 0L, `100` = 0L, `250` = 2L, `500` = 1L,
`1000` = 1L, Infinity = 3L), c(`0` = 0L, `25` = 0L, `100` = 1L,
`250` = 1L, `500` = 1L, Infinity = 4L, `0` = 0L, `25` = 0L, `100` = 1L,
`250` = 1L, `500` = 1L, Infinity = 4L))
谁能帮我解释一下我应该怎么做?
您可以使用 lapply
和 c()
。
lapply(1:length(list1), function(x) c(list1[[x]], list2[[x]]))
或mapply
与append
或c
:
mapply(append, list1, list2)
输出
[[1]]
0 25 100 250 500 1000 Infinity 0
0 0 1 1 1 1 3 0
25 100 250 500 1000 Infinity
0 0 2 1 1 3
[[2]]
0 25 100 250 500 Infinity 0 25
0 0 1 1 1 4 0 0
100 250 500 Infinity
1 1 1 4
检查它是否与您的相同list_out
:
identical(lapply(1:length(list1), function(x) c(list1[[x]], list2[[x]])), list_out)
[1] TRUE
identical(mapply(append, list1, list2), list_out)
[1] TRUE
你可以使用基础函数append
:
> append(list1[[1]], list2[[1]])
0 25 100 250 500 1000 Infinity 0 25 100
0 0 1 1 1 1 3 0 0 0
250 500 1000 Infinity
2 1 1 3
> append(list1[[2]], list2[[2]])
0 25 100 250 500 Infinity 0 25 100 250
0 0 1 1 1 4 0 0 1 1
500 Infinity
1 4
这是另一个基本的 R 解决方案。
Map(c, list1, list2)
identical(Map(c, list1, list2), list_out)
#[1] TRUE
另一种选择是map2
library(purrr)
map2(list1, list2, c)