Reputation: 438
I have three tibbles a,b,c in a vector first=c(a,b,c)
. I have another three tibbles in another vector second=c(d,e,f)
. How do I do a parallel cbind? So a should be cbinded with d, b with e, and c with f. I started out with something like
lapply(first,function(item){})
but I don't know how to take the second list into consideration in a "parallel" way.
Example-
a1<-data.frame(a=c(1,2),b=c(4,5))
a2<-data.frame(a=c(5,6),b=c(7,8))
a3<-data.frame(e=c(34,26),f=c(41,65))
a4<-data.frame(e=c(13,25),f=c(14,57))
I want to cbind a1 to a3, and a2 to a4 to produce
a b e f
1 1 4 34 41
2 2 5 26 65
and
a b e f
1 5 7 13 14
2 6 8 25 57
Upvotes: 1
Views: 191
Reputation: 887511
If we have the character vectors for objects, use mget
to return the values of the objects in a list
and then use map2
to loop over the corresponding list
elements and bind the columns with bind_cols
library(purrr)
library(dplyr)
map2(mget(first), mget(second), bind_cols)
Upvotes: 1
Reputation: 389135
We can create list of a1
, a2
and a3
, a4
and use Map
to cbind
them.
Map(cbind, list(a1, a2), list(a3 ,a4))
#[[1]]
# a b e f
#1 1 4 34 41
#2 2 5 26 65
#[[2]]
# a b e f
#1 5 7 13 14
#2 6 8 25 57
purrr
equivalent is :
purrr::map2(list(a1, a2), list(a3 ,a4), cbind)
Upvotes: 2