Skip to content

Instantly share code, notes, and snippets.

@cosi1
Created February 20, 2019 19:22
Show Gist options
  • Save cosi1/6dddde8ccc465933784c2e4ee25dd831 to your computer and use it in GitHub Desktop.
Save cosi1/6dddde8ccc465933784c2e4ee25dd831 to your computer and use it in GitHub Desktop.
Range matching in R
range_if = function(.., ..., default = NULL) {
for (expr in list(...)) {
s = substitute(expr)
if (length(s) < 3 || s[[1]] != as.symbol("~")) {
stop("Invalid argument. Use `condition ~ expression`")
}
if (isTRUE(eval(s[[2]]))) return(eval(s[[3]]))
}
default
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment