Skip to content

Instantly share code, notes, and snippets.

@timriffe
Created March 20, 2014 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timriffe/9669897 to your computer and use it in GitHub Desktop.
Save timriffe/9669897 to your computer and use it in GitHub Desktop.
aggN() aggregate vector of single age (year) data to N-year groups
#'
#' @title aggN aggregate vector of single age (year) data to N-year groups
#'
#' @description This only makes sense if N is 5 or 10 (or 2 I guess), since it uses modulo to find groups
#' Ages are assumed to start at 0 and count up in single ages.
#'
#' @param x the vector of single-age-classified data
#' @param N desired width of interval, e.g., 5 or 10
#'
#' @export
#'
aggN <- function(x,N){
age <- 0:(length(x) - 1)
tapply(x,age - age %% N, sum)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment