Daves
Daves

Reputation: 315

R mutate & gsub where pattern is based on a column

I am trying to remove pattern from var_1 using mutate() and gsub().

As gsub() only takes a string, I have to use rowwise() before the mutate(). Otherwise it will only use the first record from the pattern column.

I am wondering if there is any other method to achieve the same result without using rowwise() as it slows the process quite a bit.

test <- data.frame(
  var_1 = c('1AB', '2AB', '3C')
  ,pattern = c('AB','A','C')
)

test %>%
  dplyr::rowwise() %>%
  dplyr::mutate( result = sub(pattern, '', var_1)
          )

Desired results:

# A tibble: 3 x 4
# Rowwise: 
  var_1 var_2 pattern result
  <chr> <lgl> <chr>   <chr> 
1 1AB   FALSE AB      1     
2 2AB   TRUE  A       2B    
3 3C    FALSE C       3 

Upvotes: 3

Views: 2606

Answers (2)

akrun
akrun

Reputation: 887193

We can use map2

library(dplyr)
library(purrr)
test %>% 
      mutate(result = map2_chr(var_1, pattern, ~ sub(.y, '', .x)))

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388992

You can use stringr options which are vectorized.

Using str_remove :

library(dplyr)
library(stringr)

test %>% mutate(result = str_remove(var_1, pattern))

#  var_1 pattern result
#1   1AB      AB      1
#2   2AB       A     2B
#3    3C       C      3

this is same as using str_replace with replacement as "".

test %>%  mutate(result = str_replace(var_1, pattern, ''))

Upvotes: 7

Related Questions