基于str中匹配正则表达式的条件str_replace?

时间:2019-09-20 14:47:31

标签: r regex dplyr stringr

对于与“ regex("[:alpha:]{2}AL")”匹配的“区”列中的任何条目,我想将“ AL”替换为“ 01”。

例如:

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

我尝试过:

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

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

但是存在向量化问题。

2 个答案:

答案 0 :(得分:3)

可以吗?

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

我用\\w(一个文字字符:https://www.regular-expressions.info/shorthand.html

)替换了正则表达式

我正在使用\\1来表示将字符串替换为在(\\w{2})中捕获的第一个捕获区域,因此保留前2个字母,然后添加01

答案 1 :(得分:1)

您可以将replace更改为ifelse

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