Skip to content
Prev 326977 / 398502 Next

How to remove attributes from scale() in a matrix?

Hi Mike,
If you check ?scale

For ?scale.default?, the centered, scaled matrix.? The numeric
???? centering and scalings used (if any) are returned as attributes
???? ?"scaled:center"? and ?"scaled:scale"?

By checking the source code:
methods(scale)

getAnywhere('scale.default')

function (x, center = TRUE, scale = TRUE) 
{
??? x <- as.matrix(x)
??? nc <- ncol(x)
??? if (is.logical(center)) {
??????? if (center) {
??????????? center <- colMeans(x, na.rm = TRUE)
??????????? x <- sweep(x, 2L, center, check.margin = FALSE)
??????? }
??? }
??? else if (is.numeric(center) && (length(center) == nc)) 
??????? x <- sweep(x, 2L, center, check.margin = FALSE)
??? else stop("length of 'center' must equal the number of columns of 'x'")
??? if (is.logical(scale)) {
??????? if (scale) {
??????????? f <- function(v) {
??????????????? v <- v[!is.na(v)]
??????????????? sqrt(sum(v^2)/max(1, length(v) - 1L))
??????????? }
??????????? scale <- apply(x, 2L, f)
??????????? x <- sweep(x, 2L, scale, "/", check.margin = FALSE)
??????? }
??? }
??? else if (is.numeric(scale) && length(scale) == nc) 
??????? x <- sweep(x, 2L, scale, "/", check.margin = FALSE)
??? else stop("length of 'scale' must equal the number of columns of 'x'")
??? if (is.numeric(center)) 
??????? attr(x, "scaled:center") <- center
??? if (is.numeric(scale)) 
??????? attr(x, "scaled:scale") <- scale
??? x
}

#You can comment out the last few lines:

scale1<- function (x, center = TRUE, scale = TRUE) 
{
??? x <- as.matrix(x)
??? nc <- ncol(x)
??? if (is.logical(center)) {
??????? if (center) {
??????????? center <- colMeans(x, na.rm = TRUE)
??????????? x <- sweep(x, 2L, center, check.margin = FALSE)
??????? }
??? }
??? else if (is.numeric(center) && (length(center) == nc)) 
??????? x <- sweep(x, 2L, center, check.margin = FALSE)
??? else stop("length of 'center' must equal the number of columns of 'x'")
??? if (is.logical(scale)) {
??????? if (scale) {
??????????? f <- function(v) {
??????????????? v <- v[!is.na(v)]
??????????????? sqrt(sum(v^2)/max(1, length(v) - 1L))
??????????? }
??????????? scale <- apply(x, 2L, f)
??????????? x <- sweep(x, 2L, scale, "/", check.margin = FALSE)
??????? }
??? }
??? else if (is.numeric(scale) && length(scale) == nc) 
??????? x <- sweep(x, 2L, scale, "/", check.margin = FALSE)
??? else stop("length of 'scale' must equal the number of columns of 'x'")
??? #if (is.numeric(center)) 
??? #??? attr(x, "scaled:center") <- center
??? #if (is.numeric(scale)) 
??? #??? attr(x, "scaled:scale") <- scale
??? x
}
?x2<-scale1(x,center=TRUE,scale=TRUE)
?str(x2)
# num [1:15, 1:10] -0.2371 -0.5606 -0.8242 1.5985 -0.0164 ...

identical(x1,x2)
#[1] TRUE
A.K.



----- Original Message -----
From: C W <tmrsg11 at gmail.com>
To: arun <smartpink111 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Tuesday, July 16, 2013 6:58 PM
Subject: Re: [R] How to remove attributes from scale() in a matrix?

Arun, thanks for the quick response.? That helps.

Why does scale() give attributes?? What's the point of that?? I don't
see apply() or any similar functions do it.? Just for my curiosity.

Mike
On Tue, Jul 16, 2013 at 4:07 PM, arun <smartpink111 at yahoo.com> wrote: