Split ages into age groups defined by the split parameter. This allows for easier demographic (antimicrobial resistance) analysis.

age_groups(x, split_at = c(12, 25, 55, 75), na.rm = FALSE)

Arguments

x

age, e.g. calculated with age()

split_at

values to split x at, defaults to age groups 0-11, 12-24, 25-54, 55-74 and 75+. See Details.

na.rm

a logical to indicate whether missing values should be removed

Value

Ordered factor

Details

To split ages, the input can be:

  • A numeric vector. A vector of e.g. c(10, 20) will split on 0-9, 10-19 and 20+. A value of only 50 will split on 0-49 and 50+. The default is to split on young children (0-11), youth (12-24), young adults (25-54), middle-aged adults (55-74) and elderly (75+).

  • A character:

    • "children" or "kids", equivalent of: c(0, 1, 2, 4, 6, 13, 18). This will split on 0, 1, 2-3, 4-5, 6-12, 13-17 and 18+.

    • "elderly" or "seniors", equivalent of: c(65, 75, 85). This will split on 0-64, 65-74, 75-84, 85+.

    • "fives", equivalent of: 1:20 * 5. This will split on 0-4, 5-9, 10-14, ..., 90-94, 95-99, 100+.

    • "tens", equivalent of: 1:10 * 10. This will split on 0-9, 10-19, 20-29, ... 80-89, 90-99, 100+.

Stable lifecycle


The lifecycle of this function is stable. In a stable function, we are largely happy with the unlying code, and major changes are unlikely. This means that the unlying code will generally evolve by adding new arguments; we will avoid removing arguments or changing the meaning of existing arguments.

If the unlying code needs breaking changes, they will occur gradually. To begin with, the function or argument will be deprecated; it will continue to work but will emit an message informing you of the change. Next, typically after at least one newly released version on CRAN, the message will be transformed to an error.

Read more on our website!

On our website https://msberends.gitlab.io/AMR you can find a tutorial about how to conduct AMR analysis, the complete documentation of all functions (which reads a lot easier than here in R) and an example analysis using WHONET data.

See also

To determine ages, based on one or more reference dates, use the age() function.

Examples

ages <- c(3, 8, 16, 54, 31, 76, 101, 43, 21)

# split into 0-49 and 50+
age_groups(ages, 50)

# split into 0-19, 20-49 and 50+
age_groups(ages, c(20, 50))

# split into groups of ten years
age_groups(ages, 1:10 * 10)
age_groups(ages, split_at = "tens")

# split into groups of five years
age_groups(ages, 1:20 * 5)
age_groups(ages, split_at = "fives")

# split specifically for children
age_groups(ages, "children")
# same:
age_groups(ages, c(1, 2, 4, 6, 13, 17))

if (FALSE) {
# resistance of ciprofloxacine per age group
library(dplyr)
example_isolates %>%
  filter_first_isolate() %>%
  filter(mo == as.mo("E. coli")) %>%
  group_by(age_group = age_groups(age)) %>%
  select(age_group, CIP) %>%
  ggplot_rsi(x = "age_group")
}