If the filename
should not be part of the match, one can utilize the filename
column to avoid regex from matching:
library(dplyr)
library(purrr)
df %>%
mutate(no_filename = map2_chr(filename, text, ~gsub(.x, '', .y))) %>%
filter(grepl("^0", no_filename)) %>%
select(-no_filename)
Result:
filename text value
1 S2 S20XXXXX 0.2065314
2 S3 S30XXXXX 0.8146400
3 S4 S40XXXXX 0.8123895
4 S6 S60XXXXX 0.1111354
5 S7 S70XXXXX 0.1028646
6 S9 S90XXXXX 0.1306957
7 S9 S90XXXXX 0.3203732
8 S10 S100XXXXX 0.1876911
Note:
Notice that S100XXXXX
is matched, but not S101XXXXX
Data:
library(dplyr)
df = data.frame(filename = rep(paste0('S', 1:10), each = 5))
set.seed(123)
df = df %>%
mutate(text = paste0(filename, sample(c(0:5), 50, replace = TRUE),
paste(rep('X', 5), collapse = "")),
value = runif(50))