Performs a normalization of data, i.e., it scales variables in the range 0 -

  1. This is a special case of change_scale().

normalize(x, ...)

# S3 method for numeric
normalize(x, include_bounds = TRUE, verbose = TRUE, ...)

# S3 method for grouped_df
normalize(
  x,
  select = NULL,
  exclude = NULL,
  include_bounds = TRUE,
  verbose = TRUE,
  ...
)

# S3 method for data.frame
normalize(
  x,
  select = NULL,
  exclude = NULL,
  include_bounds = TRUE,
  verbose = TRUE,
  ...
)

Arguments

x

A numeric vector, data frame, or matrix. See details.

...

Arguments passed to or from other methods.

include_bounds

Logical, if TRUE, return value may include 0 and 1. If FALSE, the return value is compressed, using Smithson and Verkuilen's (2006) formula (x * (n - 1) + 0.5) / n, to avoid zeros and ones in the normalized variables. This can be useful in case of beta-regression, where the response variable is not allowed to include zeros and ones.

verbose

Toggle warnings and messages on or off.

select

Character vector of column names. If NULL (the default), all variables will be selected.

exclude

Character vector of column names to be excluded from selection.

Value

A normalized object.

Details

  • If x is a matrix, normalization is performed across all values (not column- or row-wise). For column-wise normalization, convert the matrix to a data.frame.

  • If x is a grouped data frame (grouped_df), normalization is performed separately for each group.

References

  • Smithson M, Verkuilen J (2006). A Better Lemon Squeezer? Maximum-Likelihood Regression with Beta-Distributed Dependent Variables. Psychological Methods, 11(1), 54–71.

See also

Other transform utilities: change_scale(), ranktransform(), standardize()

Examples

normalize(c(0, 1, 5, -5, -2))
#> [1] 0.5 0.6 1.0 0.0 0.3
normalize(c(0, 1, 5, -5, -2), include_bounds = FALSE)
#> [1] 0.50 0.58 0.90 0.10 0.34

head(normalize(trees))
#>        Girth     Height      Volume
#> 1 0.00000000 0.29166667 0.001497006
#> 2 0.02439024 0.08333333 0.001497006
#> 3 0.04065041 0.00000000 0.000000000
#> 4 0.17886179 0.37500000 0.092814371
#> 5 0.19512195 0.75000000 0.128742515
#> 6 0.20325203 0.83333333 0.142215569