Skip to content

Instantly share code, notes, and snippets.

@klmr
Last active August 29, 2015 14:01
Show Gist options
  • Save klmr/ef7e835733abff8f4b42 to your computer and use it in GitHub Desktop.
Save klmr/ef7e835733abff8f4b42 to your computer and use it in GitHub Desktop.
Python-like range operator in R, allowing to specify step size
`:` = function (a, b) {
if (inherits(a, 'xrange'))
do.call(seq, as.list(c(range(a), by = b)))
else if (inherits(a, 'factor'))
interaction(a, b, sep = ':')
else
structure(seq(a, b), class = 'xrange')
}
print.xrange = function (x)
print(as.numeric(x))
# > 1 : 5
# [1] 1 2 3 4 5
# > 1 : 10 : 2
# [1] 1 3 5 7 9
# > 1 : 5 : 2 * 2
# [1] 2 6 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment