更改R中字符串中多次出现的模式

时间:2016-03-31 18:56:38

标签: r str-replace stringr

我有一个包含一列的数据框,其中每一行代表sql select语句的一部分,例如下面:

test <-
  bind_rows(
    data.frame(text = "spend_1 + spend_2", stringsAsFactors = FALSE),
    data.frame(text = "spend_1 + spend_2 + spend_3", stringsAsFactors = FALSE),
    data.frame(text = "spend_2 - spend_3", stringsAsFactors = FALSE)
  )

print(test)

Source: local data frame [3 x 1]

                         text
                        (chr)
1           spend_1 + spend_2
2 spend_1 + spend_2 + spend_3
3           spend_2 - spend_3

我想,对于\w+的每个实例,将表别名添加到变量中。例如:

                         text   text_adj

1           spend_1 + spend_2   a.spend_1 + a.spend_2   
2 spend_1 + spend_2 + spend_3   a.spend_1 + a.spend_2 + a.spend_3
3           spend_2 - spend_3   a.spend_2 - a.spend_3

使用str_replace我可以用&#34;某些文字&#34;替换每个变量,但我无法弄清楚如何用别名+原始变量文本替换每个实例

library(stringr)

str_replace_all(text, "\\w+", "some text")

1 个答案:

答案 0 :(得分:2)

您只需要捕获模式并使用\\1引用它。例如,

test %>%
    mutate(., text2 = str_replace_all(text, "(\\w+)", "alias.\\1"))
# Source: local data frame [3 x 2]
# 
#                          text                                         text2
#                         (chr)                                         (chr)
# 1           spend_1 + spend_2                 alias.spend_1 + alias.spend_2
# 2 spend_1 + spend_2 + spend_3 alias.spend_1 + alias.spend_2 + alias.spend_3
# 3           spend_2 - spend_3                 alias.spend_2 - alias.spend_3