R-alpha: scale()
Kurt Hornik
Kurt.Hornik@ci.tuwien.ac.at
Mon, 4 Aug 1997 13:15:54 +0200
Here is a version of the function scale() which may be worth adding to
the distribution. No NA's yet, though. And yes, I could also write
documentation if necessary ...
-k
***********************************************************************
scale <- function(x, center = TRUE, scale = TRUE) {
x <- as.matrix(x)
nc <- ncol(x)
if (is.logical(center)) {
if (center)
x <- sweep(x, 2, apply(x, 2, mean))
}
else if (is.numeric(center) && (length(center) == nc))
x <- sweep(x, 2, center)
else
stop("Length of center must equal the number of columns of x")
if (is.logical(scale)) {
if (scale)
x <- sweep(x, 2, apply(x, 2, function (v)
sum(v^2) / max(1, length(v) - 1)), "/")
}
else if (is.numeric(scale) && length(scale) == nc)
x <- sweep(x, 2, scale, "/")
else
stop("Length of scale must equal the number of columns of x")
x
}
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-