Search code examples
rcbind

How to do a parallel cbind


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

Solution

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