Performs a grand-mean centering of data.

center(x, ...)

# S3 method for numeric
center(x, weights = NULL, robust = FALSE, verbose = TRUE, ...)

# S3 method for data.frame
center(
  x,
  select = NULL,
  exclude = NULL,
  weights = NULL,
  robust = FALSE,
  force = FALSE,
  append = FALSE,
  suffix = "_c",
  verbose = TRUE,
  ...
)

Arguments

x

A data frame, a (numeric or character) vector or a factor.

...

Currently not used.

weights

Can be NULL (for no weighting), or:

  • For data frames: a numeric vector of weights, or a character of the name of a column in the data.frame that contains the weights.

  • For numeric vectors: a numeric vector of weights.

robust

Logical, if TRUE, centering is done by subtracting the median from the variables. If FALSE, variables are centered by subtracting the mean.

verbose

Toggle warnings and messages.

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.

force

Logical, if TRUE, forces centering of factors as well. Factors are converted to numerical values, with the lowest level being the value 1 (unless the factor has numeric levels, which are converted to the corresponding numeric value).

append

Logical, if TRUE and x is a data frame, standardized variables will be added as additional columns; if FALSE, existing variables are overwritten.

suffix

Character value, will be appended to variable (column) names of x, if x is a data frame and append = TRUE.

Value

The centered variables.

See also

If centering within-clusters (instead of grand-mean centering) is required, see demean().

Examples

data(iris)
head(iris$Sepal.Width)
#> [1] 3.5 3.0 3.2 3.1 3.6 3.9
head(center(iris$Sepal.Width))
#> [1]  0.44266667 -0.05733333  0.14266667  0.04266667  0.54266667  0.84266667
head(center(iris))
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1   -0.7433333  0.44266667       -2.358  -0.9993333  setosa
#> 2   -0.9433333 -0.05733333       -2.358  -0.9993333  setosa
#> 3   -1.1433333  0.14266667       -2.458  -0.9993333  setosa
#> 4   -1.2433333  0.04266667       -2.258  -0.9993333  setosa
#> 5   -0.8433333  0.54266667       -2.358  -0.9993333  setosa
#> 6   -0.4433333  0.84266667       -2.058  -0.7993333  setosa
head(center(iris, force = TRUE))
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1   -0.7433333  0.44266667       -2.358  -0.9993333      -1
#> 2   -0.9433333 -0.05733333       -2.358  -0.9993333      -1
#> 3   -1.1433333  0.14266667       -2.458  -0.9993333      -1
#> 4   -1.2433333  0.04266667       -2.258  -0.9993333      -1
#> 5   -0.8433333  0.54266667       -2.358  -0.9993333      -1
#> 6   -0.4433333  0.84266667       -2.058  -0.7993333      -1