if (FALSE) {
# Creating categorical variables
ds$new_var <- makeCaseWhenVariable(
ds$x %in% c("a", "b") ~ ds$y, # can fill with a variable
ds$x %in% c("c", "d") ~ Category(name = "c or d", numeric_value = 10), # or a Category
# If none of the categories match, will be set to missing unless you
# specify an "else" case with `TRUE` in the left hand side
TRUE ~ Category(name = "catch all"),
name = "combined x and y"
)
ds$brand_x_pref <- makeCaseWhenVariable(
ds$brand[[1]] == "Brand X" ~ ds$pref[[1]],
ds$brand[[2]] == "Brand X" ~ ds$pref[[2]],
ds$brand[[3]] == "Brand X" ~ ds$pref[[3]],
name = "brand x preference"
)
ds$x_among_aware <- makeCaseWhenVariable(
ds$aware_x == "Yes" ~ ds$x,
TRUE ~ Category(name = "(Not aware)", missing = TRUE),
name = "x (among respondents aware of x)"
)
ds$new_num_var <- makeCaseWhenVariable(
ds$x %in% c("a", "b") ~ ds$z, # LHS as before, RHS can be numeric variables,
ds$x == "c" ~ ds$z * 10, # expressions,
ds$x == "d" ~ 100, # or numbers
name = "New numeric variable"
)
ds$capped_z <- makeCaseWhenVariable(
ds$z > 10 ~ 10,
TRUE ~ ds$z,
name = "Capped z"
)
# caseWhenExpr can be used inside other expressions
ds$brand_x_prefer_high <- VarDef(
selectCategories(
caseWhenExpr(
ds$brand_shown[[1]] == "Brand X" ~ ds$ratings[[1]],
ds$brand_shown[[2]] == "Brand X" ~ ds$ratings[[2]],
ds$brand_shown[[3]] == "Brand X" ~ ds$ratings[[3]]
),
c("Best", "Very Good")
),
name = "Rate X highly"
)
# Using lists in `cases` argument can be helpful when working programmatically
source_var <- ds$x
inclusion_condition <- ds$skipped_x != "Yes"
ds$x2_among_aware <- makeCaseWhenVariable(
cases = list(list(fill = source_var, expression = inclusion_condition)),
name = "x2 among aware"
)
}
Run the code above in your browser using DataLab