Learn R Programming

scf (version 1.0.5)

scf_update: Create or Alter SCF Variables

Description

Use this function to create or alter SCF variables once the raw data set has been loaded into memory using the scf_load() function. This function updates an scf_mi_survey object by evaluating transformations within each implicate, and then returning a new object with the new or amended variables.

Most of the time, you can use scf_update() to define variables based on simple logical conditions, arithmetic transformations, or categorical binning. These rules are evaluated separately in each implicate, using the same formula. However, if the transformation you want to apply depends on the distribution of the data within each implicate, such as computing an average percentile or ranking households across all implicates, this function will not suffice. In those cases, use scf_update_by_implicate() to write a custom function that operates on each implicate individually.

Usage

scf_update(object, ...)

Value

A new scf_mi_survey object with:

implicates

A list of updated data frames (one per implicate).

mi_design

A list of updated svyrep.design survey objects.

data

(If present in the original object) unchanged pooled data.

Arguments

object

A scf_mi_survey object, typically created by scf_load().

...

Named expressions assigning new or modified variables using = syntax. Each expression must return a vector of the same length as the implicate data frame.

Usage

Use scf_update() during data wrangling to clean, create, or alter variables before calculating statistics or running models. The function is useful when the analyst wishes to:

  • Recode missing values that are coded as numeric data

  • Recast variables that are not in the desired format (e.g., converting a numeric variable to a factor)

  • Create new variables based on existing ones (e.g., calculating ratios, differences, or indicators)

See Also

scf_load(), scf_update_by_implicate(), survey::svrepdesign()

Examples

Run this code
# Do not implement these lines in real analysis:
# Use functions `scf_download()` and `scf_load()`
td <- tempfile("update_")
dir.create(td)

src <- system.file("extdata", "scf2022_mock_raw.rds", package = "scf")
file.copy(src, file.path(td, "scf2022.rds"), overwrite = TRUE)
scf2022 <- scf_load(2022, data_directory = td)

# Example for real analysis: Create a binary indicator for being over age 50
scf2022 <- scf_update(scf2022,
  over50 = age > 50
)

# Example: Create a log-transformed income variable
scf2022 <- scf_update(scf2022,
  log_income = log(income + 1)
)

# Do not implement these lines in real analysis: Cleanup for package check
unlink(td, recursive = TRUE, force = TRUE)

Run the code above in your browser using DataLab