Learn R Programming

eVCGsampler (version 0.9.2)

robust_scale: Robust Scaling of Numeric and Categorical Variables

Description

Applies robust scaling to numeric and categorical variables. For numeric variables, the function centers by the median and scales by the MAD. For categorical variables with 2–4 unique levels, it applies a custom transformation to map them to numeric values.

Usage

robust_scale(x, group)

Value

A scaled numeric vector or a data frame with scaled columns.

Arguments

x

A numeric vector, factor, matrix, or data frame. If a matrix or data frame is provided, scaling is applied column-wise.

group

vector indicating which group is the TG to scale to

Details

This function is designed to make numeric and categorical variables comparable. This is an internal function that should not be used by package users.

Examples

Run this code

dat<-data.frame(x=rnorm(100, 10, 3), sex=factor(rbinom(100, 1, 0.5), labels=c("M","F")))

x<- robust_scale(dat$x, dat$sex)
round(median(x), 2)
round(mad(x), 2)

Run the code above in your browser using DataLab