I have a dataframe, from which I want to select important columns, and then filter the rows to contain specific ending.
Regex expression make it simple to define my ending value using xx$ symbol. But, how to vary over multiple possible endings (xx$, yy$)?
Dummy example:
require(dplyr)
x <- c("aa", "aa", "aa", "bb", "cc", "cc", "cc")
y <- c(101, 102, 113, 201, 202, 344, 407)
type = rep("zz", 7)
df = data.frame(x, y, type)
# Select all expressions that starts end by "7"
df %>%
select(x, y) %>%
filter(grepl("7$", y))
# It seems working when I explicitly define my variables, but I need to use it as a vector instead of values?
df %>%
select(x, y) %>%
filter(grepl("[2|7]$", y)) # need to modify this using multiple endings
# How to modify this expression, to use vector of endings (ids) instead?
ids = c(7,2) # define vector of my values
df %>%
select(x, y) %>%
filter(grepl("ids$", y)) # how to change "grepl(ids, y)??"
Expected output:
x y type
1 aa 102 zz
2 cc 202 zz
3 cc 407 zz
Example based on this question: Regular expressions (RegEx) and dplyr::filter()
grepl("[2|7]$", y). But as this is only a dummy example, I need to rewrite it to use instead a vector of variables, ie.ids = c(2,7). How to put this intogreplstatement? grepl("ids$", y) obviously does not work...df %>% select(x, y) %> filter(grepl(paste(ids, collapse="|"), y)). But I don't understand why now I did not have to specify the$at the end of the regex statement? Can you please post your comment as answer? I understand that there are many examples, but I could not imagine how to put them together... Thank you again for you help! :)paste0to add what remains,df %>% select(x, y) %> filter(grepl(paste0("(?:", paste(ids, collapse="|"), ")$"), y))selectand than rows usingfilter, which is missing from suggested answer.