标签: grepl

在dplyr中结合grepl过滤观察结果

我试图找出如何使用dplyr和过滤大型数据集中的一些观察结果grepl.grepl如果其他解决方案更优化,我不会坚持.

拿这个样本df:

df1 <- data.frame(fruit=c("apple", "orange", "xapple", "xorange", 
                          "applexx", "orangexx", "banxana", "appxxle"), group=c("A", "B") )
df1


#     fruit group
#1    apple     A
#2   orange     B
#3   xapple     A
#4  xorange     B
#5  applexx     A
#6 orangexx     B
#7  banxana     A
#8  appxxle     B
Run Code Online (Sandbox Code Playgroud)

我想要:

  1. 过滤掉以'x'开头的那些案例
  2. 过滤掉那些以'xx'结尾的案例

我已经设法弄清楚如何摆脱包含'x'或'xx'的所有东西,但不是以开头或结尾.这里是如何摆脱内部'xx'的一切(不仅仅是结束):

df1 %>%  filter(!grepl("xx",fruit))

#    fruit group
#1   apple     A
#2  orange     B
#3  xapple     A
#4 xorange     B
#5 banxana     A
Run Code Online (Sandbox Code Playgroud)

这显然是"错误的"(从我的角度来看)过滤了'appxxle'.

我从来没有完全掌握正则表达式.我一直在尝试修改代码,例如: grepl("^(?!x).*$", df1$fruit, perl = …

r filter dplyr grepl

33
推荐指数
1
解决办法
3万
查看次数

使用R中的grepl完成单词匹配

请考虑以下示例:

> testLines <- c("I don't want to match this","This is what I want to match")
> grepl('is',testLines)
> [1] TRUE TRUE
Run Code Online (Sandbox Code Playgroud)

但是,我想要的只是匹配'是',当它单独作为一个单词时.从阅读一些perl文档来看,似乎这样做的方法是使用\ b,一个锚点,可用于识别模式前后的内容,即\ bword\b匹配'word'但不匹配'sword ".所以我尝试了以下示例,使用Perl语法设置为'TRUE':

> grepl('\bis\b',testLines,perl=TRUE)
> [1] FALSE FALSE
Run Code Online (Sandbox Code Playgroud)

我正在寻找的输出是FALSE TRUE.

regex r grepl

22
推荐指数
3
解决办法
2万
查看次数

正则表达式检测所有字母字符是否为大写

如何检测字符串中的所有字母字符(> = 2个字符)是否为大写?最后,我试图过滤掉章节标题名称,即我的数据集中的行.因此,如果章节标题是"ARYA",我希望检测到,与"女王的手"相同.

这是我正在尝试但不起作用的:

library(dplyr)
library(stringr)

str_detect("THE QUEEN’S HAND", "^[[:upper:]]{2,}+$")
#> FALSE
Run Code Online (Sandbox Code Playgroud)

我需要的要求:

  • 字符数> = 2因为我最终使用它来过滤掉章节名称,但有时会出现一行"I"这个词,但这不是章节 - 它只是一个单词.虽然这可以在不同的点过滤
  • 仅检测到字母字符或撇号.有时行是"...",我不想检测到.但是,如果我使用toupper(x) == (x)解决方案,这将与"女王的手"一样被检测到.我也试图摆脱任何带有感叹号或句号的东西,比如"停止这个!"

regex r stringr grepl

15
推荐指数
5
解决办法
3031
查看次数

R如何在if语句中使用grep

在RI中想要在if语句中做类似的事情,如下面的示例,我在mix $ color列中搜索包含单词red的任何颜色,并在mix数据帧中将新变量设置为红色.

mix$newcolor <- if(grep("Red",mix$color) "red"

这里是数据帧组合的一些示例数据:

AliceBlue BlueViolet DarkRed MediumVioletRed

我收到此错误消息:

警告消息:在if(grepl("深红色",混合$ color)== TRUE)"red":条件长度> 1且仅使用第一个元素

我认为grepl应该返回一个TRUE或FALSE布尔值,所以这应该是可接受的,但我错过了一些东西(或很多).

谢谢你的帮助.

if-statement r grepl

14
推荐指数
2
解决办法
2万
查看次数

在另一个字符串向量中查找字符串向量的匹配项

我正在尝试创建一个新闻文章数据框的子集,其中至少提到一组关键字或短语的一个元素.

# Sample data frame of articles
articles <- data.frame(id=c(1, 2, 3, 4), text=c("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod", "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,", "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo", "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse"))
articles$text <- as.character(articles$text)

# Sample vector of keywords or phrases
keywords <- as.character(c("elit", "tempor incididunt", "reprehenderit"))

#   id                                                                         text
# 1 …
Run Code Online (Sandbox Code Playgroud)

grep r string-matching grepl

13
推荐指数
1
解决办法
5698
查看次数

在R中grepl以查找与任何字符串列表的匹配

在引用值列表时是否可以使用grepl参数,可能使用%in%运算符?我想获取下面的数据,如果动物名称中有"dog"或"cat",我想返回一个值,比如"keep"; 如果它没有"狗"或"猫",我想返回"丢弃".

data <- data.frame(animal = sample(c("cat","dog","bird", 'doggy','kittycat'), 50, replace = T))
Run Code Online (Sandbox Code Playgroud)

现在,如果我只是通过严格匹配值来做到这一点,比如"cat"和"dog",我可以使用以下方法:

matches <- c("cat","dog")

data$keep <- ifelse(data$animal %in% matches, "Keep", "Discard")
Run Code Online (Sandbox Code Playgroud)

但是使用grep或grepl只引用列表中的第一个参数:

data$keep <- ifelse(grepl(matches, data$animal), "Keep","Discard")
Run Code Online (Sandbox Code Playgroud)

回报

Warning message:
In grepl(matches, data$animal) :
  argument 'pattern' has length > 1 and only the first element will be used
Run Code Online (Sandbox Code Playgroud)

注意,我在搜索中看到了这个帖子,但这似乎不起作用: grep使用具有多个模式的字符向量

regex grep r grepl

13
推荐指数
3
解决办法
5万
查看次数

使用grepl搜索文本中的多个子字符串

我在R中使用grepl()来搜索我的文本中是否存在以下任一类型.我现在这样做:

grepl("Action", my_text) |
grepl("Adventure", my_text) |  
grepl("Animation", my_text) |    
grepl("Biography", my_text) |  
grepl("Comedy", my_text) |    
grepl("Crime", my_text) |  
grepl("Documentary", my_text) |  
grepl("Drama", my_text) |  
grepl("Family", my_text) |  
grepl("Fantasy", my_text) |  
grepl("Film-Noir", my_text) |  
grepl("History", my_text) |  
grepl("Horror", my_text) |  
grepl("Music", my_text) |  
grepl("Musical", my_text) |  
grepl("Mystery", my_text) |  
grepl("Romance", my_text) |  
grepl("Sci-Fi", my_text) |  
grepl("Sport", my_text) |  
grepl("Thriller", my_text) |  
grepl("War", my_text) |    
grepl("Western", my_text) 
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来编写这段代码?我可以将所有类型放在一个数组中,然后以某种方式使用grepl()它吗?

regex r grepl

13
推荐指数
1
解决办法
2万
查看次数

基于多列中直接和间接相似性对变量进行分组的快速方法

我有一个相对较大的数据集(1,750,000行,5列),其中包含具有唯一ID值的记录(第一列),由四个条件(其他4列)描述。一个小例子是:

# example
library(data.table)
dt <- data.table(id=c("a1","b3","c7","d5","e3","f4","g2","h1","i9","j6"), 
                 s1=c("a","b","c","l","l","v","v","v",NA,NA), 
                 s2=c("d","d","e","k","k","o","o","o",NA,NA),
                 s3=c("f","g","f","n","n","s","r","u","w","z"),
                 s4=c("h","i","j","m","m","t","t","t",NA,NA))
Run Code Online (Sandbox Code Playgroud)

看起来像这样:

   id   s1   s2 s3   s4
 1: a1    a    d  f    h
 2: b3    b    d  g    i
 3: c7    c    e  f    j
 4: d5    l    k  n    m
 5: e3    l    k  n    m
 6: f4    v    o  s    t
 7: g2    v    o  r    t
 8: h1    v    o  u    t
 9: i9 <NA> <NA>  w <NA>
10: j6 <NA> <NA>  z <NA>
Run Code Online (Sandbox Code Playgroud)

我的最终目标是在任何描述列上查找所有具有相同字符的记录(不考虑NA),并将它们分组为新的ID,以便我可以轻松识别重复的记录。这些ID是通过串联每行的ID来构造的。 …

optimization loops r grepl data.table

13
推荐指数
2
解决办法
238
查看次数

grep for dplyr sql table?

是否有使用类似的方法

filter(df, grepl("A|B|C",location))
Run Code Online (Sandbox Code Playgroud)

对于dplyr SQL表?在SQL中它是probalby a LIKE.我可以将SQL表转换为R数据表,但它非常大.(http://cran.r-project.org/web/packages/dplyr/vignettes/databases.html)我得到的那一刻

Error in sqliteSendQuery(conn, statement) : 
  error in statement: no such function: GREPL
Run Code Online (Sandbox Code Playgroud)

克里斯托夫

r filter dplyr grepl

11
推荐指数
1
解决办法
1267
查看次数

从包含R中特定字符的字符串向量中删除条目

我有两个字符向量:

x = {"a", "b", "c", "kt"}
y = {"abs", "kot", "ccf", "okt", "kk", "y"}
Run Code Online (Sandbox Code Playgroud)

我需要使用x从y中删除条目,以便只保留不包含任何x条目的字符串,如下所示:

y = {"kot", "kk", "y"}
Run Code Online (Sandbox Code Playgroud)

代码应适用于任何大小的向量x和y.

到目前为止,我已经尝试使用gsub和grepl,但这些只适用于单个字符串.我试图创建一个循环来做到这一点,但问题似乎比我想象的更难.当然,解决方案越复杂越好,但您可以假设在这种情况下,向量x和y最多有200个条目.

string r character gsub grepl

9
推荐指数
1
解决办法
5734
查看次数