Note: For these exercises, we need the same packages as for
the previous ones in this session: sjlabelled,
tidyverse, haven.
Also, to make sure we’re all “on the same page” before we start the exercises, we should probably also import the data once more (and remove the labels).
allbus_2021 <- read_sav("./data/allbus_2021/ZA5280_v1-0-0.sav") %>%
remove_all_labels()
ma01b, ma02, ma03,
ma04. Using the across() function, the first
thing we want to do with those variables is to convert the following
values to NA: -42, -11, -10, -9.
across() with a function from the sjlabelled
package for specifying missing values.
allbus_2021 <- allbus_2021 %>%
mutate(
across(ma01b:ma04,
~set_na(
.x,
na = c(-42, -11, -10, -9))))
xenophobia
which is the mean score of these items.
across() for this is
rowMeans(). Remember to specify that NAs
should be ignored when the mean scores are computed (if you also want to
include respondents that have not provided valid answers to all four
questions).
allbus_2021 <- allbus_2021 %>%
mutate(xenophobia = rowMeans(across(
ma01b:ma04),
na.rm = TRUE))