R/reduce_agegroups.r

Defines functions reduce_agegroups

Documented in reduce_agegroups

#' Reduce the number of age groups given a broader set of limits
#'
#' Operates on lower limits
#' @param x vector of limits
#' @param limits new limits
#' @return vector with the new age groups
#' @examples
#' reduce_agegroups(seq_len(20), c(0, 5, 10))
#' @export
reduce_agegroups <- function(x, limits) {
  ret <- x[NA]
  int <- findInterval(x, sort(limits))
  ret[!is.na(int) & int > 0] <-
    limits[int[!is.na(int) & int > 0]]
  return(ret)
}

Try the socialmixr package in your browser

Any scripts or data that you put into this service are public.

socialmixr documentation built on Oct. 29, 2022, 1:09 a.m.