Learn R Programming

dlookr (version 0.3.9)

transform: Data Transformations

Description

Performs variable transformation for standardization and resolving skewness of numerical variables.

Usage

transform(x, method = c("zscore", "minmax", "log", "log+1", "sqrt",
  "1/x", "x^2", "x^3"))

Arguments

x

numeric vector for transformation.

method

method of transformations.

Value

An object of transform class. Attributes of transform class is as follows.

  • method : method of transformation data.

    • Standardization

      • "zscore" : z-score transformation. (x - mu) / sigma

      • "minmax" : minmax transformation. (x - min) / (max - min)

    • Resolving Skewness

      • "log" : log transformation. log(x)

      • "log+1" : log transformation. log(x + 1). Used for values that contain 0.

      • "sqrt" : square root transformation.

      • "1/x" : 1 / x transformation

      • "x^2" : x square transformation

      • "x^3" : x^3 square transformation

Details

transform() creates an transform class. The `transform` class includes original data, transformed data, and method of transformation.

See vignette("transformation") for an introduction to these concepts.

See Also

summary.transform, plot.transform.

Examples

Run this code
# NOT RUN {
# Generate data for the example
carseats <- ISLR::Carseats
carseats[sample(seq(NROW(carseats)), 20), "Income"] <- NA
carseats[sample(seq(NROW(carseats)), 5), "Urban"] <- NA

# Standardization ------------------------------
advertising_minmax <- transform(carseats$Advertising, method = "minmax")
advertising_minmax
summary(advertising_minmax)
plot(advertising_minmax)

# Resolving Skewness  --------------------------
advertising_log <- transform(carseats$Advertising, method = "log")
advertising_log
summary(advertising_log)
plot(advertising_log)

# Using dplyr ----------------------------------
library(dplyr)

carseats %>%
  mutate(Advertising_log = transform(Advertising, method = "log+1")) %>%
  lm(Sales ~ Advertising_log, data = .)
# }

Run the code above in your browser using DataLab