std() computes a z-transformation (standardized and centered)
on the input.
center() centers the input.
center_if() are scoped variants of
where transformation will be applied only to those variables that match the
logical condition of
1 2 3 4 5 6 7 8 9 10
std(x, ..., robust = c("sd", "2sd", "gmd", "mad"), include.fac = FALSE, append = TRUE, suffix = "_z") std_if(x, predicate, robust = c("sd", "2sd", "gmd", "mad"), include.fac = FALSE, append = TRUE, suffix = "_z") center(x, ..., include.fac = FALSE, append = TRUE, suffix = "_c") center_if(x, predicate, include.fac = FALSE, append = TRUE, suffix = "_c")
A vector or data frame.
Optional, unquoted names of variables that should be selected for
further processing. Required, if
Character vector, indicating the method applied when
standardizing variables with
String value, will be appended to variable (column) names of
A predicate function to be applied to the columns. The
variables for which
center() also work on grouped data frames
group_by). In this case, standardization
or centering is applied to the subsets of variables in
For more complicated models with many predictors, Gelman and Hill (2007) suggest leaving binary inputs as is and only standardize continuous predictors by dividing by two standard deviations. This ensures a rough comparability in the coefficients.
x is a vector, returns a vector with standardized or
centered variables. If
x is a data frame, for
append = TRUE,
x including the transformed variables as new columns is returned;
append = FALSE, only the transformed variables will be returned.
append = TRUE and
suffix = "", recoded variables will
replace (overwrite) existing variables.
center() only return a vector, if
a vector. If
x is a data frame and only one variable is specified
...-ellipses argument, both functions do return a
data frame (see 'Examples').
Gelman A (2008) Scaling regression inputs by dividing by two
standard deviations. Statistics in Medicine 27: 2865–2873.
Gelman A, Hill J (2007) Data Analysis Using Regression and Multilevel/Hierarchical Models. Cambdridge, Cambdrige University Press: 55-57
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
data(efc) std(efc$c160age) %>% head() std(efc, e17age, c160age, append = FALSE) %>% head() center(efc$c160age) %>% head() center(efc, e17age, c160age, append = FALSE) %>% head() # NOTE! std(efc$e17age) # returns a vector std(efc, e17age) # returns a data frame # with quasi-quotation x <- "e17age" center(efc, !!x, append = FALSE) %>% head() # works with mutate() library(dplyr) efc %>% select(e17age, neg_c_7) %>% mutate(age_std = std(e17age), burden = center(neg_c_7)) %>% head() # works also with grouped data frames mtcars %>% std(disp) # compare new column "disp_z" w/ output above mtcars %>% group_by(cyl) %>% std(disp) data(iris) # also standardize factors std(iris, include.fac = TRUE, append = FALSE) # don't standardize factors std(iris, include.fac = FALSE, append = FALSE) # standardize only variables with more than 10 unique values p <- function(x) dplyr::n_distinct(x) > 10 std_if(efc, predicate = p, append = FALSE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.