1
0
mirror of https://github.com/msberends/AMR.git synced 2025-07-08 08:32:04 +02:00

(v0.7.1.9070) na.rm for age(), age_groups()

This commit is contained in:
2019-09-02 15:17:41 +02:00
parent c7be72f0ce
commit 06b6266366
19 changed files with 120 additions and 87 deletions

22
R/age.R
View File

@ -25,8 +25,9 @@
#' @param x date(s), will be coerced with \code{\link{as.POSIXlt}}
#' @param reference reference date(s) (defaults to today), will be coerced with \code{\link{as.POSIXlt}} and cannot be lower than \code{x}
#' @param exact a logical to indicate whether age calculation should be exact, i.e. with decimals. It divides the number of days of \href{https://en.wikipedia.org/wiki/Year-to-date}{year-to-date} (YTD) of \code{x} by the number of days in a year of \code{reference} (either 365 or 366).
#' @param na.rm a logical to indicate whether missing values should be removed
#' @return An integer (no decimals) if \code{exact = FALSE}, a double (with decimals) otherwise
#' @seealso \code{\link{age_groups}} to split age into age groups
#' @seealso To split ages into groups, use the \code{\link{age_groups}} function.
#' @importFrom dplyr if_else
#' @inheritSection AMR Read more on our website!
#' @export
@ -39,7 +40,7 @@
#' df$age_exact <- age(df$birth_date, exact = TRUE)
#'
#' df
age <- function(x, reference = Sys.Date(), exact = FALSE) {
age <- function(x, reference = Sys.Date(), exact = FALSE, na.rm = FALSE) {
if (length(x) != length(reference)) {
if (length(reference) == 1) {
reference <- rep(reference, length(x))
@ -79,6 +80,10 @@ age <- function(x, reference = Sys.Date(), exact = FALSE) {
if (any(ages > 120, na.rm = TRUE)) {
warning("Some ages are above 120.")
}
if (isTRUE(na.rm)) {
ages <- ages[!is.na(ages)]
}
ages
}
@ -88,6 +93,7 @@ age <- function(x, reference = Sys.Date(), exact = FALSE) {
#' Split ages into age groups defined by the \code{split} parameter. This allows for easier demographic (antimicrobial resistance) analysis.
#' @param x age, e.g. calculated with \code{\link{age}}
#' @param split_at values to split \code{x} at, defaults to age groups 0-11, 12-24, 25-54, 55-74 and 75+. See Details.
#' @param na.rm a logical to indicate whether missing values should be removed
#' @details To split ages, the input can be:
#' \itemize{
#' \item{A numeric vector. A vector of e.g. \code{c(10, 20)} will split on 0-9, 10-19 and 20+. A value of only \code{50} will split on 0-49 and 50+.
@ -102,7 +108,7 @@ age <- function(x, reference = Sys.Date(), exact = FALSE) {
#' }
#' @keywords age_group age
#' @return Ordered \code{\link{factor}}
#' @seealso \code{\link{age}} to determine ages based on one or more reference dates
#' @seealso To determine ages, based on one or more reference dates, use the \code{\link{age}} function.
#' @export
#' @inheritSection AMR Read more on our website!
#' @examples
@ -135,7 +141,7 @@ age <- function(x, reference = Sys.Date(), exact = FALSE) {
#' group_by(age_group = age_groups(age)) %>%
#' select(age_group, CIP) %>%
#' ggplot_rsi(x = "age_group")
age_groups <- function(x, split_at = c(12, 25, 55, 75)) {
age_groups <- function(x, split_at = c(12, 25, 55, 75), na.rm = FALSE) {
if (!is.numeric(x)) {
stop("`x` and must be numeric, not a ", paste0(class(x), collapse = "/"), ".")
}
@ -174,5 +180,11 @@ age_groups <- function(x, split_at = c(12, 25, 55, 75)) {
# last category
labs[length(labs)] <- paste0(split_at[length(split_at)], "+")
factor(labs[y], levels = labs, ordered = TRUE)
agegroups <- factor(labs[y], levels = labs, ordered = TRUE)
if (isTRUE(na.rm)) {
agegroups <- agegroups[!is.na(agegroups)]
}
agegroups
}