1

Input dataset is like:

test = data.frame(Var_a = c("A|B","C|D|E","F"),Var_b = c("X"))

Desired output dataset is like:

test1 = data.frame(Var_C = c("AX|BX","CX|DX|EX","FX"))

3 Answers 3

2

Here's a base R option :

splitting the string on |, pasting each word with corresponding Var_b value and collapsing the string back.

test$Var_c <- mapply(function(x, y) paste0(x, y, collapse = '|'), 
                 strsplit(test$Var_a, '|', fixed = TRUE), test$Var_b)

test

#  Var_a Var_b    Var_c
#1   A|B     X    AX|BX
#2 C|D|E     X CX|DX|EX
#3     F     X       FX
Sign up to request clarification or add additional context in comments.

Comments

1

you can separate the item to have one per row using the convenient separate_rows() from the {tidyverse}, after adding the id for the last paste.

Something like

library(tidyverse)
test <- data.frame(Var_a = c("A|B","C|D|E","F"),Var_b = c("X"))
test %>% 
  mutate(id = seq_len(nrow(test))) %>% 
  separate_rows(Var_a) %>% 
  mutate(Var_c = paste0(Var_a, Var_b)) %>% 
  group_by(id) %>% 
  summarise(Var_c = paste0(Var_c, collapse = "|"))
#> `summarise()` ungrouping output (override with `.groups` argument)
#> # A tibble: 3 x 2
#>      id Var_c   
#>   <int> <chr>   
#> 1     1 AX|BX   
#> 2     2 CX|DX|EX
#> 3     3 FX

Created on 2021-01-12 by the reprex package (v0.3.0)

Comments

0

Use gsub and lookahead regular express:

test%>%mutate(Var_C=gsub('(\\w)(?=\\||$)',paste0('\\1',Var_b),Var_a, perl=TRUE))

#  Var_a Var_b    Var_C
#1   A|B     X    AX|BX
#2 C|D|E     X CX|DX|EX
#3     F     X       FX

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.