【问题标题】:Subset rows only contain letters in R子集行仅包含 R 中的字母
【发布时间】:2017-03-15 19:37:51
【问题描述】:

我的向量有大约 3000 个观察值,例如:

clients <- c("Greg Smith", "John Coolman", "Mr. Brown", "John Nightsmith (father)", "2 Nicolas Cage")

如何子集仅包含带有字母的名称的行。例如,只有 Greg Smith、John Coolman(没有 0-9、.?:[} 等符号)。

【问题讨论】:

    标签: r subset letters


    【解决方案1】:

    我们可以使用grep 仅匹配大写或小写字母以及从字符串的开头(^)到结尾($)的空格。

    grep('^[A-Za-z ]+$', clients, value = TRUE)
    #[1] "Greg Smith"   "John Coolman"
    

    或者只使用[[:alpha:] ]+

    grep('^[[:alpha:] ]+$', clients, value = TRUE)
    #[1] "Greg Smith"   "John Coolman"
    

    【讨论】:

      猜你喜欢
      • 2021-12-10
      • 2016-01-02
      • 1970-01-01
      • 2019-03-09
      • 2021-06-09
      • 1970-01-01
      • 2018-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多