【问题标题】:How to get the center and scale after using the scale function in R在R中使用缩放功能后如何获得中心和比例
【发布时间】:2018-07-08 05:47:57
【问题描述】:

这似乎是一个愚蠢的问题,但我在网上搜索过,但仍然没有找到足够的答案。

我的问题是:假设我们有一个矩阵M,那么我们使用scale()函数,我们如何通过写一行代码来提取每列的中心和比例(我知道我们可以看到中心和尺度..),但我的矩阵有很多列,手动操作很麻烦。

有什么想法吗?非常感谢!

【问题讨论】:

    标签: r scale stat


    【解决方案1】:

    您正在寻找attributes 函数:

     set.seed(1)
     mat = matrix(rnorm(1000),,10) # Suppose you have 10 columns
     s = scale(mat) # scale your data
     attributes(s)#This gives you the means and the standard deviations:
    $`dim`
    [1] 100  10
    
    $`scaled:center`
     [1]  0.1088873669 -0.0378080766  0.0296735350  0.0516018586 -0.0391342406 -0.0445193567 -0.1995797418
     [8]  0.0002549694  0.0100772648  0.0040650015
    
    $`scaled:scale`
     [1] 0.8981994 0.9578791 1.0342655 0.9916751 1.1696122 0.9661804 1.0808358 1.0973012 1.0883612 1.0548091
    

    这些值也可以通过以下方式获得:

     colMeans(mat)
     [1]  0.1088873669 -0.0378080766  0.0296735350  0.0516018586 -0.0391342406 -0.0445193567 -0.1995797418
     [8]  0.0002549694  0.0100772648  0.0040650015
     sqrt(diag(var(mat)))
     [1] 0.8981994 0.9578791 1.0342655 0.9916751 1.1696122 0.9661804 1.0808358 1.0973012 1.0883612 1.0548091
    

    你会得到一个列表,你可以按照你想要的方式进行子集化:

    或者你可以这样做

    attr(s,"scaled:center")
     [1]  0.1088873669 -0.0378080766  0.0296735350  0.0516018586 -0.0391342406 -0.0445193567 -0.1995797418
     [8]  0.0002549694  0.0100772648  0.0040650015
    
    attr(s,"scaled:scale")
     [1] 0.8981994 0.9578791 1.0342655 0.9916751 1.1696122 0.9661804 1.0808358 1.0973012 1.0883612 1.0548091
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-01
      • 2015-10-01
      • 2010-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多