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

How to replace single word line in multiple lines?

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

Warning message: In gsub(pattern = text[lengths(gregexpr("[[:alpha:]]+", text)) == : argument 'pattern' has length > 1 and only the first element will be used

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

我使用的代码如下。

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"

一个简单的逻辑索引就可以解决这个问题,因为您要保留的词似乎位于位置 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"

能否请您尝试关注并告诉我这是否对您有帮助。

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 returns a logical vector (match or not for each element of x).

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"