I have a column with swab IDs. The normal ID nomenclature is "dryswab00001". However there are some samples as "dry00135". How would I str_detect those IDs and insert "swab" before the leading zero/number?
Data
data <- structure(list(swab_id = c("dryswab00001", "dry00002")), class = "data.frame", row.names = c(NA,
-2L))
>Solution :
You can use str_detect and str_replace in stringr to do this:
install.packages("stringr")
library("stringr")
# Use str_detect to identify IDs that need modification
update <- str_detect(data$swab_id, "^dry\\d+")
# Modify the IDs
data$swab_id[update] <- str_replace(data$swab_id[update], "^dry", "dryswab")
Final dataframe:
swab_id
1 dryswab00001
2 dryswab00002