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?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With