Is there a function that takes one dataset, one col, one operator, but several values to evaluate a condition?
v1 <- c(1:3)
v2 <- c("a", "b", "c")
df <- data.frame(v1, v2)
Options to subset (programmatically)
result <- df[df$v2 == "a" | df$v2 == "b", ]
result
1 1 a
2 2 b
Or, for more robustness
result1 <- df[ df[[2]] == "a" | df[[2]] == "b", ]
result1
v1 v2
1 1 a
2 2 b
Alternatively, for easier syntax:
library(dplyr)
result2 <- filter(df, v2 == "a" | v2 == "b")
result2
v1 v2
1 1 a
2 2 b
(Am I right to assume that I can safely use dplyr's filter() inside a function? )
I did not include subset() above as it is known to be for interactive use only.
In all the cases above, one has to repeat the condition (v2 == "a" | v2 == "b"
).
I'm looking for a function to which I could pass a vector to the argument, like c("a", "b")
because I would like to pass a large number of values, and automate the process.
Such function could perhaps be something like:
fun(df, col = v2, operator = "|", value = c("a", "b")
Thank you