These functions appear to all be working with what I can see in your data and what you've used. You do need to take into account the advice that @MrFlick provided. For nearly all functions in R, you have to use an assignment operator to actually change the object.
Whenever you make changes, you should always validate that the change was as you had expected it to be, as well. That's important!
I created an arbitrary data set with what you demonstrated in your data.
Make your questions reproducible in the future...it can be fake data or just a small snapshot; check it out: making R reproducible questions.
library(tidyverse)
library(survey)
set.seed(357)
genZ_prep <- data.frame(
life_satisf = sample(rep(c("Sehr zufrieden", "Zufrieden",
"Weniger zufrieden", "Gar nicht zufrieden"), 100), 300),
mat_satisf = sample(rep(c("Selten", "Nie", "Häufig",
"Gelegentlich"), each = 100), 300),
state = sample(rep(c("Baden-Württemberg","Bavaria","Berlin","Brandenburg",
"Bremen","Hamburg","Hesse","Lower Saxony"), 50), 300)
)
Then I used what you had coded to modify the data. However, I used an assignment operator to save the changes and I put both case_when
calls in one mutate
call. (You can have two; I just combined it when I was coding is all.)
genZ <- genZ_prep %>% # recoding levels of satisfaction (very to not at all)
mutate(life_satisf = factor(case_when(
life_satisf %in% c("Sehr zufrieden", "Zufrieden") ~ 1,
life_satisf %in% c("Weniger zufrieden","Gar nicht zufrieden") ~ 0),
levels = c(1, 0),
labels = c("satisfied", "unsatisfied")),
mat_satisf = factor(case_when( # recode levels of satis (never to often)
mat_satisf %in% c("Selten", "Nie") ~ 1,
mat_satisf %in% c("Häufig", "Gelegentlich") ~ 0),
levels = c(1, 0),
labels = c("yes", "no"))) # yes is rare or never; no is often or occasionally
Were you aware that yes
is set to rarely or never? I would have thought that you would have set yes
to occasionally or often...it's not my data! Do it your way. I just thought I would mention it, in case you had not realized that.
The next part's important--validate the changes.
# validate changes
funModeling::df_status(genZ)
# variable q_zeros p_zeros q_na p_na q_inf p_inf type unique
# 1 life_satisf 0 0 0 0 0 0 factor 2
# 2 mat_satisf 0 0 0 0 0 0 factor 2
# 3 state 0 0 0 0 0 0 character 8
levels(genZ$life_satisf)
# [1] "satisfied" "unsatisfied"
levels(genZ$mat_satisf)
# [1] "yes" "no"
Then I ran the svydesign
and svymean
. However, I only have these three variables, so there isn't going to be very meaningful information from these calls.
genSavy <- svydesign(data = genZ,
id = ~ 1,
strata = ~ state)
summary(genSavy)
# Stratified Independent Sampling design (with replacement)
# svydesign(data = genZ_prep, id = ~1, strata = ~state)
# Probabilities:
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 1 1 1 1 1 1
# Stratum Sizes:
# Baden-Württemberg Bavaria Berlin Brandenburg Bremen Hamburg Hesse
# obs 35 39 34 40 34 39 38
# design.PSU 35 39 34 40 34 39 38
# actual.PSU 35 39 34 40 34 39 38
# Lower Saxony
# obs 41
# design.PSU 41
# actual.PSU 41
# Data variables:
# [1] "life_satisf" "mat_satisf" "state"
genSm <- svymean(~mat_satisf, design = genSavy, na.rm = T)
genSm
# mean SE
# mat_satisfyes 0.49667 0.029
# mat_satisfno 0.50333 0.029
More satisfied than unsatisfied with whatever mat_satisf
is in this random set I created.