Log-transform Variables in a Data Frame
Source:R/margot_log_transform_vars.R
margot_log_transform_vars.Rd
This function applies a log(x + 1) transformation to specified variables in a data frame. It handles NA values, allows for exceptions, and can be applied to variables with specific prefixes.
Usage
margot_log_transform_vars(
data,
vars,
exceptions = character(0),
prefix = "log_",
keep_original = TRUE
)
Arguments
- data
A data frame to process.
- vars
A character vector of variable names or a tidyselect helper (e.g., starts_with("hours_")).
- exceptions
A character vector of variable names to exclude from transformation.
- prefix
A string to prepend to the names of transformed variables. Default is "log_".
- keep_original
Logical. If TRUE, keeps both original and transformed variables. If FALSE, replaces original variables. Default is TRUE.
Examples
df <- data.frame(
hours_work = c(0, 1, 5, NA),
hours_sleep = c(6, 7, 8, 9),
income = c(1000, 2000, 3000, 4000)
)
transformed_df <- margot_log_transform_vars(df,
vars = c(starts_with("hours_"), "income"),
exceptions = "hours_work")
#> Loading required package: crayon
#> Loading required package: cli
#>
#> Attaching package: ‘cli’
#> The following object is masked from ‘package:crayon’:
#>
#> num_ansi_colors
#> Loading required package: knitr
#>
#> === Log-transforming Variables ===
#>
#> Initial Data Summary:
#> Total variables: 3
#> Total observations: 4
#> Exceptions specified: 1
#>
#> Variables to be Transformed:
#> Total variables to transform: 2
#> List of variables to transform:
#>
#>
#> |Variable |
#> |:-----------|
#> |hours_sleep |
#> |income |
#>
#> Transformed Variables:
#>
#>
#> |Original |Transformed |
#> |:-----------|:---------------|
#> |hours_sleep |log_hours_sleep |
#> |income |log_income |
#>
#> Original variables kept as keep_original = TRUE
#>
#> Final Data Summary:
#> Total variables: 5
#> Variables transformed: 2
#> Variables excluded: 1
#>
#> Log transformation completed successfully! 👍
#> ================================================================================