R grep:有AND运算符吗? [英] R grep: is there an AND operator?

查看:143
本文介绍了R grep:有AND运算符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下数据框:

  User.Id标签
34234 imageUploaded,people.jpg,更多,逗号,分隔,东西
34234 imageUploaded
12345 people.jpg

我如何使用grep(或其他工具)来只抓取包含imageUploaded和people的行?换句话说,我如何创建一个包含字符串imageUploaded和people.jpg的行的子集,无论订单如何。

我试过了:

  data.people< -data [grep(imageUploaded | people.jpg,results $ Tags),] 
data.people< -data [grep(imageUploaded?= people.jpg,results $ Tags),]

是否有AND运算符?或者也许另一种方式来获得预期的结果?

解决方案

感谢这个答案,这个正则表达式似乎工作。你想使用 grepl(),它返回一个逻辑索引到你的数据对象中。我不会声称完全理解正则表达式的内部运作,但不管:

  x < -  c(imageUploaded ,people.jpg,more,comma,separated,stuff,imageUploaded,people.jpg)

grepl((?=。* imageUploaded)(?=。* people \ \.jpg),x,perl = TRUE)
#-----
[1] TRUE FALSE FALSE


Suppose I have the following data frame:

User.Id    Tags
34234      imageUploaded,people.jpg,more,comma,separated,stuff
34234      imageUploaded
12345      people.jpg

How might I use grep (or some other tool) to only grab rows that include both "imageUploaded" and "people"? In other words, how might I create a subset that includes just the rows with the strings "imageUploaded" AND "people.jpg", regardless of order.

I have tried:

data.people<-data[grep("imageUploaded|people.jpg",results$Tags),]
data.people<-data[grep("imageUploaded?=people.jpg",results$Tags),]

Is there an AND operator? Or perhaps another way to get the intended result?

解决方案

Thanks to this answer, this regex seems to work. You want to use grepl() which returns a logical to index into your data object. I won't claim to fully understand the inner workings of the regex, but regardless:

x <- c("imageUploaded,people.jpg,more,comma,separated,stuff", "imageUploaded", "people.jpg")

grepl("(?=.*imageUploaded)(?=.*people\\.jpg)", x, perl = TRUE)
#-----
[1]  TRUE FALSE FALSE

这篇关于R grep:有AND运算符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆