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

C W tmrsg11 at gmail.com
Wed Jul 17 01:17:21 CEST 2013


Thanks, very clear!

On Tue, Jul 16, 2013 at 7:08 PM, arun <smartpink111 at yahoo.com> wrote:
> 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:
>> HI,
>> Try:
>> x1<-scale(x,center=TRUE,scale=TRUE)
>> str(x1)
>> # num [1:15, 1:10] -0.2371 -0.5606 -0.8242 1.5985 -0.0164 ...
>> # - attr(*, "scaled:center")= num [1:10] 50.2 50 49.8 49.8 50.3 ...
>>  #- attr(*, "scaled:scale")= num [1:10] 1.109 0.956 0.817 0.746 1.019 ...
>>
>>  attr(x1,"scaled:center")<-NULL
>>  attr(x1,"scaled:scale")<-NULL
>> str(x1)
>>  #num [1:15, 1:10] -0.2371 -0.5606 -0.8242 1.5985 -0.0164 ...
>> A.K.
>>
>>
>>
>>
>> ----- Original Message -----
>> From: C W <tmrsg11 at gmail.com>
>> To: r-help <r-help at r-project.org>
>> Cc:
>> Sent: Tuesday, July 16, 2013 3:59 PM
>> Subject: [R] How to remove attributes from scale() in a matrix?
>>
>> Hi list,
>>
>> I am using scale() to standardize a distribution?  But why does it
>> give me attributes attached to the data?  I just want a standardized
>> matrix, that is all.
>>
>> library(mvtnorm)
>>> x <- rmvnorm(15, mean=rep(50, 10))
>>> x
>>           [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]
>>   [,8]     [,9]
>> [1,] 51.17519 52.34341 49.63084 47.99234 51.63113 50.91391 49.36819
>> 49.23901 51.17377
>> [2,] 50.57039 49.17210 48.64395 49.03940 49.65761 49.93840 49.94883
>> 50.69044 49.57632
>> [3,] 50.64811 50.21503 50.13786 49.15879 48.51550 50.19444 50.23710
>> 50.98040 51.37032
>> [4,] 49.22797 49.66445 49.93287 48.63681 50.49457 50.33302 52.29552
>> 49.98424 51.04724
>> [5,] 49.72099 50.84510 50.60976 49.60883 53.59509 49.14728 50.23134
>> 49.09141 49.23780
>> [6,] 49.49126 50.90938 49.67140 50.08951 49.79854 49.03711 50.26037
>> 50.24975 48.26958
>> [7,] 51.12384 47.92778 50.60112 49.01554 49.47515 50.12756 51.65216
>> 49.21998 49.63808
>> [8,] 51.45123 50.44037 50.01039 50.27511 49.97658 51.63002 50.37156
>> 50.02685 48.95423
>> [9,] 51.16989 50.16200 51.17724 50.71678 50.79565 50.27128 51.05608
>> 49.61165 47.81732
>> [10,] 49.54263 49.93501 49.71762 49.33378 51.44935 51.53775 50.54346
>> 49.98333 49.59422
>> [11,] 51.16497 49.82914 49.08821 51.02918 49.67663 49.53498 50.26647
>> 49.48569 50.94504
>> [12,] 51.16827 50.50244 49.13003 49.00155 50.26457 48.85465 49.11593
>> 50.58031 51.14926
>> [13,] 48.26216 49.94866 48.62526 49.11995 50.40082 49.25359 48.57677
>> 50.66760 49.44108
>> [14,] 49.82530 49.17352 50.05588 50.51265 51.04926 50.32474 49.78180
>> 50.48349 49.92431
>> [15,] 50.55772 49.84691 47.95021 50.24911 49.85335 50.73062 51.48718
>> 51.36693 50.18307
>>          [,10]
>> [1,] 50.13859
>> [2,] 51.54920
>> [3,] 49.23230
>> [4,] 50.92683
>> [5,] 50.97708
>> [6,] 50.78799
>> [7,] 50.53913
>> [8,] 49.30832
>> [9,] 49.43606
>> [10,] 49.42060
>> [11,] 50.21002
>> [12,] 51.94848
>> [13,] 49.41352
>> [14,] 52.24064
>> [15,] 51.19474
>>> scale(x, center=TRUE, scale=TRUE)
>>             [,1]       [,2]         [,3]        [,4]        [,5]
>> [,6]        [,7]
>> [1,]  0.8890317  2.3390090 -0.040395734 -1.86089754  1.00159470
>> 0.92533476 -0.99715965
>> [2,]  0.2452502 -0.9109703 -1.190404546 -0.63771097 -0.66104313
>> -0.21446975 -0.40514793
>> [3,]  0.3279747  0.1578297  0.550427419 -0.49823662 -1.62323564
>> 0.08468695 -0.11121941
>> [4,] -1.1837031 -0.4064112  0.311551281 -1.10802250  0.04407804
>> 0.24660932  1.98754311
>> [5,] -0.6589074  0.8035298  1.100314901  0.02749734  2.65618150
>> -1.13883336 -0.11709623
>> [6,] -0.9034419  0.8694088  0.006865424  0.58904255 -0.54231158
>> -1.26755243 -0.08749646
>> [7,]  0.8343705 -2.1861602  1.090250934 -0.66558751 -0.81476108
>> 0.00655050  1.33157578
>> [8,]  1.1828615  0.3887665  0.401888014  0.80585326 -0.39231482
>> 1.76205433  0.02587038
>> [9,]  0.8833860  0.1034854  1.761589113  1.32181395  0.29773018
>> 0.17447101  0.72381232
>> [10,] -0.8487569 -0.1291363  0.060728488 -0.29381647  0.84844800
>> 1.65423826  0.20113824
>> [11,]  0.8781560 -0.2376361 -0.672712386  1.68676651 -0.64501776
>> -0.68583461 -0.08127647
>> [12,]  0.8816611  0.4523675 -0.623990804 -0.68193230 -0.14968994
>> -1.48074503 -1.25436403
>> [13,] -2.2117715 -0.1151423 -1.212190165 -0.54361597 -0.03490809
>> -1.01461386 -1.80409304
>> [14,] -0.5478718 -0.9095198  0.454889973  1.08335795  0.51138447
>> 0.23693367 -0.57544865
>> [15,]  0.2317608 -0.2194204 -1.998811911  0.77548830 -0.49613484
>> 0.71117025  1.16336204
>>             [,8]        [,9]       [,10]
>> [1,] -1.2666791  1.17934107 -0.35061189
>> [2,]  0.8423439 -0.28600703  1.06389274
>> [3,]  1.2636749  1.35963155 -1.25940610
>> [4,] -0.1838111  1.06327078  0.43980595
>> [5,] -1.4811512 -0.59653247  0.49019839
>> [6,]  0.2019965 -1.48467432  0.30058779
>> [7,] -1.2943281 -0.22935616  0.05103467
>> [8,] -0.1218950 -0.85664924 -1.18316969
>> [9,] -0.7252082 -1.89953387 -1.05507780
>> [10,] -0.1851315 -0.26958168 -1.07058564
>> [11,] -0.9082374  0.96952049 -0.27897948
>> [12,]  0.6823136  1.15685832  1.46428187
>> [13,]  0.8091562 -0.41005981 -1.07768018
>> [14,]  0.5416286  0.03320871  1.75724879
>> [15,]  1.8253278  0.27056365  0.70846058
>> attr(,"scaled:center")
>> [1] 50.33999 50.06102 49.66551 49.58529 50.44225 50.12196 50.34618
>> 50.11074 49.88811
>> [10] 50.48823
>> attr(,"scaled:scale")
>> [1] 0.9394453 0.9757930 0.8581604 0.8560117 1.1869812 0.8558562
>> 0.9807762 0.6882016
>> [9] 1.0901550 0.9972455
>>
>>
>> Also,
>>> attributes(x) <- NULL
>> will not work since this is matrix not vector.
>>
>> Thanks,
>> Mike
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>



More information about the R-help mailing list