删除以大写字母开头的单词

时间:2017-11-21 23:31:29

标签: r extract uppercase

如何删除数据集中以大写字母开头的所有单词?

例如:

d <- c("nice", "cat", "Cat", "Dog")

应该会产生c("nice", "cat")

(是的,在问这个问题之前,我在网上看了很长时间。我确定答案很简单,但我无法弄清楚它的正则表达式语法。)

3 个答案:

答案 0 :(得分:5)

d[!substr(d,1,1) %in% LETTERS]
[1] "nice" "cat"

我很好奇哪个选项会更快。以下是使用带有100,000个单词的字符串向量和带有100万个单词的字符串向量的计时。我还使用stringi包以及@Hugh建议的其他选项添加了一个选项。

library(microbenchmark)
library(stringi)
library(data.table)
library(hutils)

set.seed(3)
d <- replicate(1e5, paste(sample(c(letters,LETTERS),sample(3:15,1)), collapse="")) 

microbenchmark(substr=d[!substr(d,1,1) %in% LETTERS],
               grepl=d[!grepl("^[A-Z]", d)],
               grepl_perl=d[!grepl("^[A-Z]", d, perl = TRUE)],
               grep=grep("^[A-Z]", d, invert = TRUE, value = TRUE),
               stri_detect=d[!stri_detect(d, regex="^[A-Z]")],
               hutils=d[substr(d, 0, 1) %notin% LETTERS],
               data.table=d[!substr(d,1,1) %chin% LETTERS], times=50)
Unit: milliseconds
        expr      min       lq     mean   median       uq      max neval   cld
      substr 19.34844 21.12396 23.66050 22.57122 25.83950 34.19547    50 ab   
       grepl 25.07439 27.64983 30.31913 28.46804 31.40705 44.55779    50    d 
  grepl_perl 19.90326 21.68584 25.45138 22.87602 25.09937 97.97515    50  bc  
        grep 23.65844 26.01204 28.72596 27.35598 29.84097 57.92622    50   cd 
 stri_detect 29.16854 30.56955 35.62350 32.13257 39.58317 68.51851    50     e
      hutils 19.08427 20.92759 22.73886 21.80824 23.56090 30.38251    50 ab   
  data.table 17.26040 18.80886 21.23428 20.12133 21.63160 46.63104    50 a
set.seed(3)
d <- replicate(1e6, paste(sample(c(letters,LETTERS),sample(3:15,1)), collapse="")) 
Unit: milliseconds
        expr      min       lq     mean   median       uq      max neval   cld
      substr 165.1537 179.1681 192.0479 186.7607 194.0660 331.4462    50 ab   
       grepl 249.7070 260.9464 273.2971 275.0886 283.0254 302.8629    50    d 
  grepl_perl 193.3224 200.4336 213.0202 209.6039 218.6055 362.2129    50   c  
        grep 236.9711 252.3330 269.3016 272.6767 279.6774 375.7031    50    d 
 stri_detect 264.5809 281.9253 291.0088 289.7235 301.6924 321.7426    50     e
      hutils 169.7349 179.9313 197.5622 190.5230 196.2092 346.2719    50  bc  
  data.table 151.4252 160.2967 177.1575 167.4981 175.2483 310.1474    50 a

答案 1 :(得分:5)

查找(grep)以大写字母(^)开头([A-Z])的字词,但返回其他所有内容(invert = TRUE

grep("^[A-Z]", c("nice", "cat", "Cat", "Dog"), invert = TRUE, value = TRUE)
# [1] "nice" "cat"

答案 2 :(得分:5)

您可以使用grepl()生成逻辑索引,并将其用于子集(其中^标记字符串的开头):

d[!grepl("^[A-Z]", d)]
相关问题