This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Merge two comma seperated column in one frame in R

How can i merge two or more column which are seperated with";" in one column using with R? Thanks

r merge seperated column

Can you please post some example or expected output?

Output

X           Y               XY
a;b;c;      x;y;z;          <b>ax</b></br><b>by</b></br><b>cz</b></br>
a2;b2;c2;   x2;y2;z2;       <b>a2|x2</b></br><b>b2|y2</b></br><b>c2|z2</b></br>
a3;b3;c3;   x3;y3;z3;       <b>a3|x3</b></br><b>b3|y3</b></br><b>c3|z3</b></br>

1 answer

library(tidyverse)
data <- tibble(x  = c("a;b;c;","a2;b2;c2;","a3;b3;c3;") , y = c("x;y;z;","x2;y2;z2;","x3;y3;z3;"))

out <- data %>% mutate(pp = map(seq_along(x) , function(.) { 
        x_split <- unlist(strsplit(x[.] , ";"))
        y_split <- unlist(strsplit(y[.] , ";"))
        paste0(x_split , y_split , collapse  = "</b></br><b>")
        } )) %>% tidyr::unnest()

> out
# A tibble: 3 x 3
  x         y         pp                                  
  <chr>     <chr>     <chr>                               
1 a;b;c;    x;y;z;    ax</b></br><b>by</b></br><b>cz      
2 a2;b2;c2; x2;y2;z2; a2x2</b></br><b>b2y2</b></br><b>c2z2
3 a3;b3;c3; x3;y3;z3; a3x3</b></br><b>b3y3</b></br><b>c3z3

Worked very well, thank you.

Log in to answer this question.