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
You can use
stringr
options which are vectorized.Using
str_remove
:this is same as using
str_replace
with replacement as""
.