# NOT RUN {
x <- rnorm(100, 2, 1)
x2 <- rbinom(100, 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
gscale(data = mtcars, binary.inputs = "-0.5/0.5") # loops through each numeric column
# Specified vars in data frame
gscale(c("hp", "wt", "vs"), data = mtcars, binary.inputs = "center")
wts <- runif(100, 0, 1)
mtcars$weights <- wts[1:32]
# Weighted inputs
gscale(x, weights = wts)
# If using a weights column of data frame, give its name
gscale(data = mtcars, weights = weights) # will skip over mtcars$weights
# If using a weights column of data frame, can still select variables
gscale(x = c("hp", "wt", "vs"), data = mtcars, weights = weights)
# Survey designs
library(survey)
data(api)
## Create survey design object
dstrat <- svydesign(id = ~1,strata = ~stype, weights = ~pw, data = apistrat,
fpc=~fpc)
dstrat$variables$binary <- rbinom(200, 1, 0.5) # Creating test binary variable
gscale(data = dstrat, binary.inputs = "-0.5/0.5")
gscale(c("api00","meals","binary"), data = dstrat, binary.inputs = "-0.5/0.5")
# }
Run the code above in your browser using DataLab