Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R grep: is there an AND operator?

Tags:

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?

like image 983
Rob Avatar asked Nov 02 '12 00:11

Rob


People also ask

How are Regexpr Gregexpr and Regexec different than grep Grepl?

Description. grep , grepl , regexpr , gregexpr , regexec and gregexec search for matches to argument pattern within each element of a character vector: they differ in the format of and amount of detail in the results. sub and gsub perform replacement of the first and all matches respectively.

What is the use of grep () Grepl () substr ()?

17.4 grepl() grepl() returns a logical vector indicating which element of a character vector contains the match. For example, suppose we want to know which states in the United States begin with word “New”. Here, we can see that grepl() returns a logical vector that can be used to subset the original state.name vector.

How does grep work in R?

grep() function in R Language is used to search for matches of a pattern within each element of the given string. Parameters: pattern: Specified pattern which is going to be matched with given elements of the string. x: Specified string vector.

What does regular expression grep utility mean?

The grep command is one of the most useful commands in a Linux terminal environment. The name grep stands for “global regular expression print”. This means that you can use grep to check whether the input it receives matches a specified pattern.


1 Answers

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 
like image 184
Chase Avatar answered Oct 07 '22 00:10

Chase