简体   繁体   中英

R - grepl conditions on vector to exclude exact match

I am struggling to properly exclude variables when I use grepl .

Imagine my vector is

 vec = c("POP MUSIC",  "TOP THE POPS", "POPEYE", "MARY POPPINS") 

I want to grepl POP when I visually recognise pop music. In my example I want then to exclude "POPEYE", "MARY POPPINS" .

How could I do something like ? And why this line of code does not work ?

vec[ grepl("POP", vec ) & grepl("^POPEY$", vec ) & grepl("^MARY POPPINS$", vec ) ] 

desired results

"POP MUSIC" "TOP THE POPS"

thanks

You can use the following grep solution:

vec = c("POP MUSIC",  "TOP THE POPS", "POPEYE", "MARY POPPINS") 
grep("(?i)\\bPOPS?\\b", vec, value = TRUE)

See IDEONE demo

The regex (?i)\\\\bPOPS?\\\\b matches a whole word POP or POPS in a case-insensitive way (due to (?i) ) and if found, the value is returned (due to value=TRUE ).

You may need to adjust the regex as per your needs (eg (?i)\\\\bPOP(S|PING)?\\\\b to also allow popping ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM