Rbind R中两个或多个列表中的对应元素
Rbind corresponding elements in two or more lists in R
我有 3 个列表,每个列表有 500 个元素。这里出于演示目的,我有 2 个列表,每个列表有 1 个元素:
structure(list(timeseries = c(1, 7, 59), t = c(1, 3, 7)), .Names = c("timeseries", "t"), row.names = c(NA, 3L), class = "data.frame")
structure(list(timeseries = c(5, 6, 7), t = c(8, 9, 10)), .Names = c("timeseries", "t"), row.names = c(NA, 3L), class = "data.frame")
我的目标是将列表 1 中的第一个元素与列表 2 和 3 中的第一个元素绑定。然后,列表 1 中的第二个元素与列表 2 和 3 中的第二个元素绑定。依此类推。
在我的例子中,我最终会得到一个这种形式的列表
structure(list(timeseries = c(1,7,59,5, 6, 7), t = c(1,3,7,8, 9, 10)), .Names = c("timeseries", "t"), row.names = c(NA, 6L), class = "data.frame")
我该怎么做?
谢谢!
****编辑*** 预期结果的改进示例。我有a和b。我想获得C.
a<-list(structure(list(timeseries = c(1, 7, 59), t = c(1, 3, 7)), .Names = c("timeseries", "t"), row.names = c(NA, 3L), class = "data.frame"),
structure(list(timeseries = c(1, 7, 59), t = c(1, 3, 7)), .Names = c("timeseries", "t"), row.names = c(NA, 3L), class = "data.frame"))
b<-list(structure(list(timeseries = c(2, 3, 5), t = c(2, 4, 6)), .Names = c("timeseries", "t"), row.names = c(NA, 3L), class = "data.frame"),
structure(list(timeseries = c(60, 70, 80), t = c(20, 30, 40)), .Names = c("timeseries", "t"), row.names = c(NA, 3L), class = "data.frame"))
c<-list(structure(list(timeseries = c(1, 7, 59, 2,3, 5), t = c(1, 3, 7, 2, 4, 6)), .Names = c("timeseries", "t"), row.names = c(NA, 6L), class = "data.frame"),
structure(list(timeseries = c(1, 7, 59, 60, 70, 80), t = c(1, 3, 7, 20, 30, 40)), .Names = c("timeseries", "t"), row.names = c(NA, 6L), class = "data.frame"))
假设a
和b
的长度相同我们可以做
lapply(seq_along(a), function(x) rbind(a[[x]], b[[x]]))
#[[1]]
# timeseries t
#1 1 1
#2 7 3
#3 59 7
#4 2 2
#5 3 4
#6 5 6
#[[2]]
# timeseries t
#1 1 1
#2 7 3
#3 59 7
#4 60 20
#5 70 30
#6 80 40
seq_along
生成从 1 到对象长度的序列。如果你这样做
seq_along(a) #you would get output as
#[1] 1 2
因为 length(a)
是 2。所以我们 rbind
数据帧一个接一个 rbind(a[[1]], b[[1]])
首先,然后 rbind(a[[2]], b[[2]])
等等。 lapply
确保最终输出是一个列表。
试试 map2 函数:
purrr::map2(a,b,rbind) -> d
identical(c,d)
# [1] TRUE
我有 3 个列表,每个列表有 500 个元素。这里出于演示目的,我有 2 个列表,每个列表有 1 个元素:
structure(list(timeseries = c(1, 7, 59), t = c(1, 3, 7)), .Names = c("timeseries", "t"), row.names = c(NA, 3L), class = "data.frame")
structure(list(timeseries = c(5, 6, 7), t = c(8, 9, 10)), .Names = c("timeseries", "t"), row.names = c(NA, 3L), class = "data.frame")
我的目标是将列表 1 中的第一个元素与列表 2 和 3 中的第一个元素绑定。然后,列表 1 中的第二个元素与列表 2 和 3 中的第二个元素绑定。依此类推。
在我的例子中,我最终会得到一个这种形式的列表
structure(list(timeseries = c(1,7,59,5, 6, 7), t = c(1,3,7,8, 9, 10)), .Names = c("timeseries", "t"), row.names = c(NA, 6L), class = "data.frame")
我该怎么做?
谢谢!
****编辑*** 预期结果的改进示例。我有a和b。我想获得C.
a<-list(structure(list(timeseries = c(1, 7, 59), t = c(1, 3, 7)), .Names = c("timeseries", "t"), row.names = c(NA, 3L), class = "data.frame"),
structure(list(timeseries = c(1, 7, 59), t = c(1, 3, 7)), .Names = c("timeseries", "t"), row.names = c(NA, 3L), class = "data.frame"))
b<-list(structure(list(timeseries = c(2, 3, 5), t = c(2, 4, 6)), .Names = c("timeseries", "t"), row.names = c(NA, 3L), class = "data.frame"),
structure(list(timeseries = c(60, 70, 80), t = c(20, 30, 40)), .Names = c("timeseries", "t"), row.names = c(NA, 3L), class = "data.frame"))
c<-list(structure(list(timeseries = c(1, 7, 59, 2,3, 5), t = c(1, 3, 7, 2, 4, 6)), .Names = c("timeseries", "t"), row.names = c(NA, 6L), class = "data.frame"),
structure(list(timeseries = c(1, 7, 59, 60, 70, 80), t = c(1, 3, 7, 20, 30, 40)), .Names = c("timeseries", "t"), row.names = c(NA, 6L), class = "data.frame"))
假设a
和b
的长度相同我们可以做
lapply(seq_along(a), function(x) rbind(a[[x]], b[[x]]))
#[[1]]
# timeseries t
#1 1 1
#2 7 3
#3 59 7
#4 2 2
#5 3 4
#6 5 6
#[[2]]
# timeseries t
#1 1 1
#2 7 3
#3 59 7
#4 60 20
#5 70 30
#6 80 40
seq_along
生成从 1 到对象长度的序列。如果你这样做
seq_along(a) #you would get output as
#[1] 1 2
因为 length(a)
是 2。所以我们 rbind
数据帧一个接一个 rbind(a[[1]], b[[1]])
首先,然后 rbind(a[[2]], b[[2]])
等等。 lapply
确保最终输出是一个列表。
试试 map2 函数:
purrr::map2(a,b,rbind) -> d
identical(c,d)
# [1] TRUE