Create a new variable that has values when specific conditions are met. 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. This is commonly a Crunch variable but may be a string or numeric value, depending on the type of variable you're constructing.

conditionalTransform(
  ...,
  data,
  else_condition = NA,
  type = NULL,
  categories = NULL,
  formulas = NULL
)

Arguments

...

a list of conditions to evaluate (as formulas, see Details) as well as other properties to pass to the new conditional variable (i.e. alias, description)

data

a Crunch dataset object to use

else_condition

a default value to use if none of the conditions are true (default: NA)

type

a character that is either "categorical", "text", "numeric" what type of output should be returned? If NULL, the type of the source variable will be used. (default: NULL) The source variables will be converted to this type if necessary.

categories

a vector of characters if type="categorical", these are all of the categories that should be in the resulting variable, in the order they should be in the resulting variable or a set of Crunch categories.

formulas

a list of conditions to evaluate (as formulas, see Details). If specified, ... must not contain other formulas specifying conditions.

Value

a Crunch VariableDefinition

Details

The type of the new variable can depend on the type(s) of the source variable(s). By default (type=NULL), the type of the new variable will be the type of all of the source variables (that is, if all of the source variables are text, the new variable type will be text, if all of the source variables are categorical, the new variable will be categorical). If there are multiple types in the source variables, the result will be a text variable. The default behavior can be overridden by specifying type = "categorical", "text", or "numeric".

conditionalTransform is similar to makeCaseVariable; however, conditionalTransform can use other Crunch variables as a source of a variable, whereas, makeCaseVariable can only use characters. This additional power comes at a cost: makeCaseVariable can be executed entirely on Crunch servers, so no data needs to be downloaded or uploaded to/from the local R session. conditionalTransform on the other hand will download the data necessary to construct the new variable.

Examples

if (FALSE) {

ds$cat_opinion <- conditionalTransform(pet1 == "Cat" ~ Opinion1,
    pet2 == "Cat" ~ Opinion2,
    pet3 == "Cat" ~ Opinion3,
    data = ds,
    name = "Opinion of Cats"
)
}