viswath
viswath

Reputation: 11

Concatenate String with Variables

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"))

Upvotes: 1

Views: 63

Answers (3)

Shenglin Chen
Shenglin Chen

Reputation: 4554

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

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388797

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

Upvotes: 2

aurelien
aurelien

Reputation: 868

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)

Upvotes: 1

Related Questions