if (FALSE) {
ds$derived_v1 <- ds$v1 + 5
derivation(ds$derived_v1)
# Crunch expression: v1 + 5
derivation(ds$derived_v1) <- ds$v1 + 10
derivation(ds$derived_v1)
# Crunch expression: v1 + 10
is.derived(ds$derived_v1)
# TRUE
# to integrate or instantiate the variable in place (remove the link between
# variable v1 and the derivation) you can:
derivation(ds$derived_v1) <- NULL
# after integrating, the derived variable is no longer derived.
is.derived(ds$derived_v1)
# FALSE
# Derivations can be updated with arbitrary expressions.
# Consider a numeric case variable that combines weights
# calculated separately in a separate variable
# for each of several waves:
ds$weight <- makeCaseWhenVariable(
ds$wave == 1 ~ ds$weight_wave1,
ds$wave == 2 ~ ds$weight_wave2,
ds$wave == 3 ~ ds$weight_wave3,
name = "Weight"
)
# When a new wave is added, update the derivation
# of the weight to add the new condition and source
# column.
derivation(ds$weight) <- caseWhenExpr(
ds$wave == 1 ~ ds$weight_wave1,
ds$wave == 2 ~ ds$weight_wave2,
ds$wave == 3 ~ ds$weight_wave3,
ds$wave == 4 ~ ds$weight_wave4
)
}
Run the code above in your browser using DataLab