绑定R中两个或多个列表中的对应元素 [英] Rbind corresponding elements in two or more lists in R

查看:238
本文介绍了绑定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)

结构(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),类= data.frame)

我该怎么做?

谢谢!



****编辑***预期结果的改进示例。我有a和b。我想获取C。

  a< -list(structure(list(time(timeseries = c(1,7,59), t = c(1,3,7)),.names = c( timeseries, t),row.names = c(NA,3L),class = data.frame),
结构(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,4) ,6)),.names = c( timeseries, t),row.names = c(NA,3L),class = data.frame),
结构(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),
结构( 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]]
#时间序列t
#1 1 1
#2 7 3
#3 59 7
#4 2 2
#5 3 4
#6 5 6

#[[2]]
#时间序列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)#,您将获得
#[1] 1 2 $ b $的输出b

因为 length(a)为2,所以我们 rbind 数据帧一个接一个 rbind(a [[1]],b [[1]]),然后 rbind(a [[2]],b [[2]])等。 lapply 确保最终输出是列表。


I have 3 lists, each with 500 elements. Here for demonstrative purposes, I have 2 lists with 1 element each:

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

My aim is to rbind the first element in list 1 with the first element in list 2 and 3. Then, the second element in list 1 with the second element in list 2 and 3. And so on.

In my example, I would end up with a list of this form

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

How do I do this?

Thank you!

****EDIT*** Improved example of the intended outcome. I have a and b. I want to obtain 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"))

解决方案

Assuming length of a and b is the same we can do

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 generates sequence from 1 to length of the object. If you do

seq_along(a) #you would get output as
#[1] 1 2

as length(a) is 2. So we rbind the dataframe one by one rbind(a[[1]], b[[1]]) first, then rbind(a[[2]], b[[2]]) and so on. lapply ensures the final output is a list.

这篇关于绑定R中两个或多个列表中的对应元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆