| ageToAgeGroup {dembase} | R Documentation |
Convert a vector of exact ages to a vector of age groups.
The age groups are formatted in
the way expected by functions such as Counts and
Values.
ageToAgeGroup(age, breaks = seq(0, 100, 5), firstOpen = FALSE, lastOpen = TRUE)
age |
A vector of exact ages. A numeric vector, or a vector than can be coerced to numeric. |
breaks |
A vector of breaks, specifying the points dividing the age groups. |
firstOpen |
Logical. Whether the first age group is "open",
i.e. has no lower bound. Defaults to |
lastOpen |
Logical. Whether the last age group is "open",
i.e. has no upper bound. Defaults to |
‘Exact’ ages can be numbers such as 6.238 or
77.13, but are more typically integers
such as 6 or 77.
If age is a factor, then ageToAgeGroup will coerce
it to a character vector before trying to coerce it to numeric.
See below for an example.
By default, ageToAgeGroup creates 5-year age groups.
See below for examples of other groupings.
A factor, the same length as age.
timeToPeriod. seq (in combination with c)
is useful for creating complicated breaks arguments.
age <- c(22, 18, 4, 0, 89, 103, 7)
## 5-year age groups, 0-4, 5-9, ..., 95-99, 100+
ageToAgeGroup(age)
## 1-year age groups, 0, 1, ..., 89, 90+
ageToAgeGroup(age, breaks = 0:90)
## age groups 0, 1-4, 5-9, 10-14, ..., 85+
ageToAgeGroup(age, breaks = c(0, 1, seq(5, 85, 5)))
## last age group closed
ageToAgeGroup(age = c(0, 17, 14, 3, 9),
breaks = seq(0, 20, 5),
lastOpen = FALSE)