Last active
August 29, 2015 14:01
-
-
Save MrFlick/3bf8671c2984238feb70 to your computer and use it in GitHub Desktop.
between.R: checks if a number is between two other numbers (inclusive)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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