Create a categorical or numeric variable based on conditions
Conditions are specified using a series of formulas: the left-hand side is
the condition that must be true (a CrunchLogicalExpr) and the right-hand
side is where to get the value if the condition on the left-hand side is
true. When creating a categorical variable, the right-hand side must be
a Category or a categorical CrunchVariable or CrunchExpression, while
for numeric variables it is a single number or variable or expression.
makeCaseWhenVariable(..., data = NULL, cases = NULL, name, type = NULL) caseWhenExpr(..., data = NULL, cases = NULL, type = NULL)
| ... | formulas where the left hand side is a  | 
| data | A CrunchDataset to use if variable aliases are left bare in the formulas. | 
| cases | A list of formulas that match the description in  | 
| name | For  | 
| type | The type of the variable to output (either "categorical" or "numeric"), only required if all fills are expressions and so their type cannot be guessed automatically. | 
makeCaseWhenVariable() returns a VariableDefinition and
caseWhenExpr() returns an expression
## Not run: 
# 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"
)
## End(Not run)Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.