#' Convenience functions for interval recodes
#'
#' Get recode assigments for even intervals of discrete numeric values compatible
#' with \link[car]{recode}.
#'
#' @param from,to A \code{numeric value} for the beginning and the end of the interval.
#' @param width The width of the interval, e.g. 5 (default) for intervals 0-5.
#'
#' @return A \code{character} vector of recode assignments compatible with \link[car]{recode}
#' @export
#' @examples
#' \dontrun{
#' x <- round(runif(100, 0, 100), 0)
#' recodes <- generate_recodes(0, 100, 10)
#'
#' library(car)
#' recode(x, recodes = recodes)
#'}
generate_recodes <- function(from, to, width = 5){
paste(sapply(seq(from, to - width, 2 * width), function(x) {
first <- paste0(x, ":", x + width, "='", x, "-", x + width, "'")
second <- paste0(x + width + 1, ":", x + (2 * width), "='", x + width + 1, "-", x + (2 * width), "'")
paste(first, second, sep = "; ", collapse = "; ")
}), collapse = "; ")
}
#' Convenience functions for interval recodes
#'
#' Get interval labels for even intervals of discrete numeric values compatible
#' with \link{cut}.
#'
#' @param from,to A \code{numeric value} for the beginning and the end of the interval.
#' @param width The width of the interval, e.g. 5 (default) for intervals 0-5.
#'
#' @return A \code{character} vector of interval labels compatible with \link{cut}
#' @export
#' @examples
#' \dontrun{
#' x <- round(runif(100, 0, 100), 0)
#' labels <- interval_labels(0, 100, 10)
#'
#' cut(x, breaks = seq(0, 100, 10), labels = labels)
#'}
interval_labels <- function(from, to, width = 5){
labs <- lapply(seq(from, to - width, 2 * width), function(x) {
c(paste0(x, "-", x + width), paste0(x + width + 1, "-", x + (2 * width)))
})
return(unlist(labs))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.