derivation {crunch}R Documentation

Get or set a derived variable's expression

Description

Get a derived variable's derivation formula as a CrunchExpr with derivation(variable). Set (change) a derived variable's derivation with derivation(variable) <- expression.

Usage

derivation(x)

derivation(x) <- value

is.derived(x)

is.derived(x) <- value

## S4 method for signature 'CrunchVariable'
derivation(x)

## S4 replacement method for signature 'CrunchVariable,ANY'
derivation(x) <- value

## S4 replacement method for signature 'CrunchVariable,'NULL''
derivation(x) <- value

## S4 method for signature 'CrunchVariable'
is.derived(x)

## S4 replacement method for signature 'CrunchVariable,logical'
is.derived(x) <- value

Arguments

x

a variable

value

a CrunchExpr to be used as the derivation (for the setter only) or NULL to integrate a derived variable. For is.derived, FALSE can be used to integrate a derived variable.

Details

To break a derivation link between a derived variable and the originating variable, set the derivation value of the derived variable to NULL with derivation(variable) <- NULL

is.derived can be used to see if a variable is derived or not. Additionally setting a derived variable's is.derived to FALSE will break the derivation link between two variables.

Value

a CrunchExpr of the derivation for derivation; a logical for is.derived; the variable given in x for ⁠is.derived<-⁠ returns

Examples

## Not run: 

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
)

## End(Not run)

[Package crunch version 1.30.4 Index]