'Create dummy variable with survey package
I want to transform a variable into a dummy using the survey package.
I have a complex sample design defined by:
library(survey)
prestratified_design <- svydesign(
id = ~ PSU ,
strata = ~ STRAT,
data = data,
weights = ~ weight ,
nest = TRUE)
The dataset has a variable for education with 8 different categories:
# A tibble: 8 x 3
education n prop
<int> <int> <dbl>
1 1 2919 20.8
2 2 5551 39.5
3 3 447 3.18
4 4 484 3.45
5 5 3719 26.5
6 6 91 0.65
7 9 790 5.63
8 10 39 0.28
I want to create a dummy variable for categories 5 & 10 == 1 and others == 0.
I know that I have to use the update function, but I don't know how to use if in the survey package. I have tried:
prestratified_design <- update(
prestratified_design,
dummy_educ = as.numeric (education == 5 & education == 10)
but it obviously didn't work.
thank you!
Solution 1:[1]
You can create dummy variables in R via ifelse()
if the number of categories is two.
df$dummy_educ = with(df, ifelse(education == 5 | education == 10, 1, 0))
If the categories are more, you can use dplyr::case_when()
, and if you are creating dummies from factor variable model.matrix()
is fast and the best.
Solution 2:[2]
In order any new variable takes in count the complex design, you don't need to update your data set (in your example data
), but you have to update your survey design adding the new variable. You must use the survey::update()
function.
Following your example, try with the code below:
prestratified_design <- update(prestratified_design,
dummy_educ = as.integer(education == 5 | education == 10))
Good luck with that!.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Eyayaw |
Solution 2 | malvarado |