Skip to contents

This function takes a dataset and adds a new column that contains age groups.

Usage

construct_age_group(dataset, age_col, group_size = 5, new_col = "fert_age_grp")

Arguments

dataset

A data frame containing at least one column with age values.

age_col

The name of the column containing age values.

group_size

An integer specifying the size of the age groups. Default is 5.

new_col

The name of the new column to store the age groups. Default is "fert_age_grp".

Value

A data frame with the new column added, containing age groups.

Examples

df <- data.frame(id = 1:10, age = c(23, 45, 34, 56, 29, 12, 67, 44, 33, 55))
new_df <- construct_age_group(df, age_col = "age", group_size = 10)
print(new_df)
#>    id age fert_age_grp
#> 1   1  23        20-29
#> 2   2  45        40-49
#> 3   3  34        30-39
#> 4   4  56        50-59
#> 5   5  29        20-29
#> 6   6  12        10-19
#> 7   7  67        60-69
#> 8   8  44        40-49
#> 9   9  33        30-39
#> 10 10  55        50-59