Learn R Programming

sjmisc (version 2.5.0)

std: Standardize and center variables

Description

std() computes a z-transformation (standardized and centered) on the input. center() centers the input.

Usage

std(x, ..., include.fac = TRUE, append = FALSE, suffix = "_z")

center(x, ..., include.fac = TRUE, append = FALSE, suffix = "_c")

Arguments

x

A vector or data frame.

...

Optional, unquoted names of variables that should be selected for further processing. Required, if x is a data frame (and no vector) and only selected variables from x should be processed. You may also use functions like : or dplyr's select_helpers. See 'Examples' or package-vignette.

include.fac

Logical, if TRUE, factors will be converted to numeric vectors and also standardized or centered.

append

Logical, if TRUE and x is a data frame, x including the new variables as additional columns is returned; if FALSE (the default), only the new variables are returned.

suffix

String value, will be appended to variable (column) names of x, if x is a data frame. If x is not a data frame, this argument will be ignored. The default value to suffix column names in a data frame depends on the function call:

  • recoded variables (rec()) will be suffixed with "_r"

  • recoded variables (recode_to()) will be suffixed with "_r0"

  • dichotomized variables (dicho()) will be suffixed with "_d"

  • grouped variables (split_var()) will be suffixed with "_g"

  • grouped variables (group_var()) will be suffixed with "_gr"

  • standardized variables (std()) will be suffixed with "_z"

  • centered variables (center()) will be suffixed with "_c"

Value

A vector with standardized or centered variables. If x is a data frame, only the transformed variables will be returned.

Examples

Run this code
# NOT RUN {
data(efc)
std(efc$c160age) %>% head()
std(efc, e17age, c160age) %>% head()

center(efc$c160age) %>% head()
center(efc, e17age, c160age) %>% head()

# NOTE!
std(efc$e17age) # returns a vector
std(efc, e17age) # returns a tibble

# works with mutate()
library(dplyr)
efc %>%
  select(e17age, neg_c_7) %>%
  mutate(age_std = std(e17age), burden = center(neg_c_7)) %>%
  head()

# }

Run the code above in your browser using DataLab