如何在多行中替换单个字线?

时间:2018-07-03 06:49:05

标签: r regex gsub

我有一些字符串的数据框。有些行有一个单词,我想用空白代替。我能够检索到该词,但是在替换它们时,我会收到警告消息

  

警告消息:在gsub(pattern =   text [lengths(gregexpr(“ [[:::]] +”,text))==:参数   'pattern'的长度> 1,将仅使用第一个元素

只有第一个单词被替换,其余单词保持原样。我要替换数据框中的所有单个单词。

我正在使用的代码如下。

text <- c("Because I could not stop for Death -",
          "Word1",
          "He kindly stopped for me -",
          "Word2",
          "The Carriage held but just Ourselves - ",
          "word3",
          "and Immortality")

gsub(pattern = text[lengths(gregexpr("[[:alpha:]]+", text)) == 1], "", text)

我期望下面的输出。

"Because I could not stop for Death -",
          "He kindly stopped for me -",
          "The Carriage held but just Ourselves - ",
          "and Immortality"

3 个答案:

答案 0 :(得分:1)

在这里,简单的逻辑索引就可以解决问题,因为您要保留的单词似乎位于1、3、5 ...等位置,即

text[c(TRUE, FALSE)]
#[1] "Because I could not stop for Death -"    "He kindly stopped for me -"             
#[3] "The Carriage held but just Ourselves - " "and Immortality"

答案 1 :(得分:1)

a=gsub("^\\w+$","",text)
[1] "Because I could not stop for Death -"    ""                                       
[3] "He kindly stopped for me -"              ""                                       
[5] "The Carriage held but just Ourselves - " ""                                       
[7] "and Immortality"   

grep("\\w",a,value = T)
[1] "Because I could not stop for Death -"    "He kindly stopped for me -"             
[3] "The Carriage held but just Ourselves - " "and Immortality"  

或者您可以简单地完成

grep("\\w+\\s",text,value = T)
[1] "Because I could not stop for Death -"    "He kindly stopped for me -"             
[3] "The Carriage held but just Ourselves - " "and Immortality"  

答案 2 :(得分:0)

能否请您尝试以下操作,如果有帮助,请告诉我。

text <- c("Because I could not stop for Death -",
          "Word1",
          "He kindly stopped for me -",
          "Word2",
          "The Carriage held but just Ourselves - ",
          "word3",
          "and Immortality")

获取OP所需输出的代码:

text[!grepl("[Ww]ord[0-9]+", text)] 

输出如下。

[1] "Because I could not stop for Death -"    "He kindly stopped for me -"             
[3] "The Carriage held but just Ourselves - " "and Immortality"

对于帮助页面上的grepl

  

grepl返回一个逻辑向量(x的每个元素是否匹配)。