简体   繁体   中英

How to concatenate / combine series of columns in R?

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.

在此处输入图像描述

Attempting something to this effect:

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

Thanks in advance!

This one did the job

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM