Last active
August 29, 2015 14:08
-
-
Save ramhiser/3768154c1bc53f1d6d17 to your computer and use it in GitHub Desktop.
Cuts a vector into factors with pretty levels
This file contains 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
#' 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, ...) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires itertools2.