Description Usage Arguments Details Value See Also Examples
View source: R/miscellaneous-functions.R
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
.
1 | 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.
1 2 3 4 5 6 7 8 9 10 11 | 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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.