Skip to content

Instantly share code, notes, and snippets.

@ramhiser
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save ramhiser/3768154c1bc53f1d6d17 to your computer and use it in GitHub Desktop.

Select an option

Save ramhiser/3768154c1bc53f1d6d17 to your computer and use it in GitHub Desktop.
Cuts a vector into factors with pretty levels
#' Cuts a vector into factors with pretty levels
#'
#' @param x numeric vectory
#' @param breaks numeric vector of two ore more unique cut points
#' @param collapse character string to collapse factor labels
#' @param ... arguments passed to \code{\link[base]{cut}}
#' @return A \code{\link{factor}} is returned
#'
#' @examples
#' set.seed(42)
#' x <- runif(n=50, 0, 50)
#' cut_pretty(x, breaks=pretty(x))
cut_pretty <- function(x, breaks, collapse=" to ", ...) {
it_breaks <- itertools2::ipairwise(breaks)
breaks_pretty <- sapply(it_breaks, paste, collapse=collapse)
cut(x, breaks=breaks, labels=breaks_pretty, ...)
}
@ramhiser
Copy link
Copy Markdown
Author

ramhiser commented Nov 7, 2014

Requires itertools2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment