我想在包含R的包含文本的列中的第一个句号后加上单词

时间:2019-03-29 11:49:36

标签: r

我想要第二句话的文字。

text="I need to go to mall today. I want to purchase clothes. Also, I want to buy shoes."

预期结果:

I want to purchase clothes. Also, I want to buy shoes.

3 个答案:

答案 0 :(得分:0)

strsplit()的可能性:

sapply(strsplit(text, ". ", fixed = TRUE), 
       function(x) paste(x[2:length(x)], collapse = ". "))

[1] "I want to purchase clothes. Also, I want to buy shoes."

答案 1 :(得分:0)

我们还可以使用stringr::str_match捕获第一个点之后的所有内容。

stringr::str_match(text, "\\.(.*)")[, 2]
#[1] " I want to purchase clothes. Also, I want to buy shoes."

[, 2]用于获取捕获组。

答案 2 :(得分:0)

我们可以将base Rsub一起使用,以匹配非.后跟.的字符和空格(如果有的话),并用空格({{1} })

""

,而sub("^[^.]+\\.\\s*", "", text) #[1] "I want to purchase clothes. Also, I want to buy shoes." 中的等效选项将为

tidyverse