Skip to content

Instantly share code, notes, and snippets.

@MrFlick
Last active August 29, 2015 14:01
Show Gist options
  • Save MrFlick/3bf8671c2984238feb70 to your computer and use it in GitHub Desktop.
Save MrFlick/3bf8671c2984238feb70 to your computer and use it in GitHub Desktop.
between.R: checks if a number is between two other numbers (inclusive)
between <- function(x, a, b) {
if(missing(b)) {
if(length(a)==2) {
a<-t(a)
}
} else {
a <- unname(cbind(a,b))
}
a<-t(apply(a,1,sort, na.last=T))
a[,1] <= x & x <= a[,2]
}
between(5,1,10)
between(5,c(1,10))
between(1:10, 4, 6)
between(1:10, c(4, 6))
between(1:3, c(0,4,2), 5)
between(7, 10, 6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment