# NOT RUN {
x <- rnorm(10, 2, 1)
x2 <- rbinom(10, 1, .5)
# Basic use
gscale(x)
# Normal standardization
gscale(x, n.sd = 1)
# Scale only
gscale(x, scale.only = TRUE)
# Center only
gscale(x, center.only = TRUE)
# Binary inputs
gscale(x2, binary.inputs = "0/1")
gscale(x2, binary.inputs = "full") # treats it like a continous var
gscale(x2, binary.inputs = "-0.5/0.5") # keep scale, center at zero
gscale(x2, binary.inputs = "center") # mean center it
# Data frame as input
# loops through each numeric column
gscale(data = mtcars, binary.inputs = "-0.5/0.5")
# Specified vars in data frame
gscale(mtcars, vars = c("hp", "wt", "vs"), binary.inputs = "center")
# Weighted inputs
wts <- runif(10, 0, 1)
gscale(x, weights = wts)
# If using a weights column of data frame, give its name
mtcars$weights <- runif(32, 0, 1)
gscale(mtcars, weights = weights) # will skip over mtcars$weights
# If using a weights column of data frame, can still select variables
gscale(mtcars, vars = c("hp", "wt", "vs"), weights = weights)
# Survey designs
if (requireNamespace("survey")) {
library(survey)
data(api)
## Create survey design object
dstrat <- svydesign(id = ~1, strata = ~stype, weights = ~pw,
data = apistrat, fpc=~fpc)
# Creating test binary variable
dstrat$variables$binary <- rbinom(200, 1, 0.5)
gscale(data = dstrat, binary.inputs = "-0.5/0.5")
gscale(data = dstrat, vars = c("api00","meals","binary"),
binary.inputs = "-0.5/0.5")
}
# }
Run the code above in your browser using DataLab