library(tidyverse)
library(sjlabelled)
gp_covid <-
read_csv2("./data/ZA5667_v1-1-0.csv") %>%
set_na(na = c(-99, -77, -33, 98))
1
Create a new binary variable called married
in the gp_covid
dataframe that has the value 1 if the individual is married and 0 if not.
As there are no missing values in the variable, you can simply use ifelse()
for this.
2
Let’s create another new variable. This time, it should be a character variable named 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”.
You can use the 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.