【问题标题】:Remove multiple spaces but leave single space in R删除多个空格但在 R 中保留单个空格
【发布时间】:2021-03-10 00:27:59
【问题描述】:

我有如下文字。

"The comm   unity is here to help you wi   th specific co   ding"

我想删除句子中的三个空格,同时留下一个空格。

我试过了

gsub(text, "   ", "")

但它不起作用。

【问题讨论】:

    标签: r gsub spacing


    【解决方案1】:

    要删除一个单词的三个空格,请使用:

    x <- "The comm   unity is here to help you wi   th specific co   ding"
    output <- gsub("\\b[ ]{3}\\b", "", x)
    output
    
    [1] "The community is here to help you with specific coding"
    

    【讨论】:

    • 对不起。我意识到这个例句有问题,所以我改变了它。
    【解决方案2】:

    这是否有效,删除多个空格:

    st <- "The comm   unity is here to help you wi   th specific co   ding"
    st
    [1] "The comm   unity is here to help you wi   th specific co   ding"
    gsub('\\s{2,}','',st)
    [1] "The community is here to help you with specific coding"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-11
      • 1970-01-01
      相关资源
      最近更新 更多