How to concatenate / combine series of columns in R?

Viewed 32

I have two column series - A series and B series.

df <- data.frame(A_1_1 =  'a',
                 A_2_1 =  'b',
                 A_3_1 =  'c',
                 B_0_1 =  'x',
                 B_0_2 =  'y',
                 B_0_3 =  'z'
)

I need to concatenate the A series with B series in the same sequence, to get the desired output.

enter image description here

Attempting something to this effect:

df %>% str_c(starts_with("A_"),"-",starts_with("B_"))

Thanks in advance!

1 Answers

This one did the job

df %>% {map2_dfc(select(.,starts_with("A_")),
                 select(.,starts_with("B_")),
                       function(x,y){
                         str_c(x,"-",y)
                       })} %>% View()
Related