# Reaggregating some data obtained from the 2021 UK census
head(pop_dat)
# Each row of the data is for the same region so we can drop some columns
# `age_category` and `value` columns
dat <- subset(pop_dat, select = c(age_category, value))
# Add the lower bounds to the data
dat <- transform(
dat,
lower_bound = as.integer(sub("\\[([0-9]+), .+)", "\\1", age_category))
)
# Now recategorise to the desired age intervals
with(
dat,
reaggregate_counts(
bounds = lower_bound,
counts = value,
new_bounds = c(0, 1, 5, 15, 25, 45, 65)
)
)
Run the code above in your browser using DataLab