Skip to content

Instantly share code, notes, and snippets.

@jeffwong
Last active December 14, 2015 19:29
Show Gist options
  • Save jeffwong/5136796 to your computer and use it in GitHub Desktop.
Save jeffwong/5136796 to your computer and use it in GitHub Desktop.
Scale data frame with both continuous and binary variables
isBinaryVariable = function(x) {
apply(x, 2, function(j) {
return (identical(range(unique(j)), c(0,1)))
})
}
scaleBinary = function(j) {
p = mean(j)
j.sd = sqrt(p*(1-p))
(j - p) / j.sd
}
myscale = function(x, scale.binary=T, ...) {
binary = isBinaryVariable(x)
if (!scale.binary) {
if ("data.table" %in% class(x)) x[,!binary,with=F] = scale(x[,!binary,with=F], ...)
else x[,!binary] = scale(x[,!binary], ...)
}
else {
if ("data.table" %in% class(x)) {
x[,!binary,with=F] = scale(x[,!binary,with=F], ...)
x[,binary,with=F] = apply(x[,binary,with=F],2,scaleBinary)
}
else {
x[,!binary] = scale(x[,!binary], ...)
x[,binary,with=F] = apply(x[,binary,with=F],2,scaleBinary)
}
return (x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment