Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

dplyr - Rbind corresponding elements in two or more lists in R

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"))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just try the map2 function :

purrr::map2(a,b,rbind) -> d
identical(c,d)
# [1] TRUE

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...