library(tidyverse)
library(sjlabelled)
gp_covid <-
read_csv2("./data/ZA5667_v1-1-0.csv") %>%
set_na(na = c(-99, -77, -33, 98))
married
in the gp_covid
dataframe that has the value 1 if the individual is married and 0 if not.
ifelse()
for this.
gp_covid <- gp_covid %>%
mutate(married = ifelse(marstat == 1, 1, 0))
age_3cat
that has the following unique values representing the respective age categories: “up to 40 years”, “41 to 60 years” and “older than 60 years”.
between()
helper function in combination with the dplyr
function for conditional variable creation/transformation. The required existing variable is called age_cat
. As a side note: In reality, you would probably would want to have such a variable as an ordered factor, but for the sake of simplicity, we’ll stick with character variable here.
gp_covid <- gp_covid %>%
mutate(age_3cat = case_when(
between(age_cat, 1, 4) ~ "up to 40 years",
between(age_cat, 5, 7) ~ "41 to 60 years",
age_cat > 7 ~ "older than 60 years"
))