Shira
Shira

Reputation: 129

conditional str_replace based on matching regex within mutate?

For any entries of the column "district" that match regex("[:alpha:]{2}AL"), I would like to replace the "AL" with "01".

For example:

df <- tibble(district = c("NY14", "MT01", "MTAL", "PA10", "KS02", "NDAL", "ND01", "AL02", "AL01"))

I tried:

  df %>% mutate(district=replace(district, 
                          str_detect(district, regex("[:alpha:]{2}AL")), 
                          str_replace(district,"AL","01"))) 

and

  df %>% mutate(district=replace(district, 
                          str_detect(district, regex("[:alpha:]{2}AL")), 
                          paste(str_sub(district, start = 1, end = 2),"01",sep = "")) 

but there is a vectorization problem.

Upvotes: 2

Views: 1146

Answers (2)

BENY
BENY

Reputation: 323226

You can change the replace to ifelse

ifelse( str_detect(df$district, regex("[:alpha:]{2}AL")), 
         str_replace(df$district,"AL","01"),df$district)

Upvotes: 3

Jonny Phelps
Jonny Phelps

Reputation: 2717

Is this ok?

str_replace_all(string=df$district,
                pattern="(\\w{2})AL",
                replacement="\\101")

I replaced the regex with \\w, a word character: https://www.regular-expressions.info/shorthand.html

I am using \\1 to indicate replace the string with the first captured region, which is captured in the (\\w{2}) so keep the first 2 letters then add the 01

Upvotes: 3

Related Questions