Merge two comma seperated column in one frame in R
1
0
Entering edit mode
3.0 years ago
ersan ▴ 10

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

R merge seperated column • 608 views
ADD COMMENT
2
Entering edit mode

Can you please post some example or expected output?

ADD REPLY
0
Entering edit mode

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>
ADD REPLY
1
Entering edit mode
3.0 years ago
Chirag Parsania ★ 1.9k
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
ADD COMMENT
0
Entering edit mode

Worked very well, thank you.

ADD REPLY

Login before adding your answer.

Traffic: 2795 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6