代码之家  ›  专栏  ›  技术社区  ›  doug

如何使用“sweep”功能

  •  88
  • doug  · 技术社区  · 14 年前

    sweep 经常使用。 有时当一个更简单的函数已经足够时使用它(例如。, apply ), 花相当多的时间逐步完成它所在的代码块。

    我可以复制的事实 打扫 我不明白 的核心用例,以及经常使用此函数的事实表明它非常有用。

    背景:

    是R标准库中的函数;其论点是:

    sweep(x, MARGIN, STATS, FUN="-", check.margin=T, ...)
    
    # x is the data
    # STATS refers to the summary statistics which you wish to 'sweep out'
    # FUN is the function used to carry out the sweep, "-" is the default
    

    应用 虽然 要求 还有一个参数, STATS .

    打扫 返回 形状相同 应用

    打扫 行动中:

    # e.g., use 'sweep' to express a given matrix in terms of distance from 
    # the respective column mean
    
    # create some data:
    M = matrix( 1:12, ncol=3)
    
    # calculate column-wise mean for M
    dx = colMeans(M)
    
    # now 'sweep' that summary statistic from M
    sweep(M, 2, dx, FUN="-")
    
         [,1] [,2] [,3]
    [1,] -1.5 -1.5 -1.5
    [2,] -0.5 -0.5 -0.5
    [3,]  0.5  0.5  0.5
    [4,]  1.5  1.5  1.5
    

    .

    请不要背诵或链接到R文档、邮件列表或任何“主要”R来源——假设我读过它们。我感兴趣的是有经验的R程序员/分析师如何使用 打扫

    5 回复  |  直到 7 年前
        1
  •  96
  •   Rekyt    5 年前

    sweep() 通常在按行或按列操作矩阵时使用,该操作的另一个输入是每行/每列的不同值。按行还是按列操作是按边距定义的,例如 apply() . 我称之为“其他输入”的值是由STATS定义的。 因此,对于每一行(或每一列),您将从STATS中获取一个值并在FUN定义的操作中使用。

    sweep (M, 1, c(1: 4), "+")
    

    坦白地说,我也不理解R文档中的定义,我只是通过查找示例来学习的。

        2
  •  16
  •   Brad Horn    11 年前

    sweep()非常适合系统地逐列或逐行操作大型矩阵,如下所示:

    > print(size)
         Weight Waist Height
    [1,]    130    26    140
    [2,]    110    24    155
    [3,]    118    25    142
    [4,]    112    25    175
    [5,]    128    26    170
    
    > sweep(size, 2, c(10, 20, 30), "+")
         Weight Waist Height
    [1,]    140    46    170
    [2,]    120    44    185
    [3,]    128    45    172
    [4,]    122    45    205
    [5,]    138    46    200
    

        3
  •  6
  •   James King    11 年前

    cov.wt ,用于计算加权协方差矩阵。我正在看r3.0.1中的代码。在这里 sweep 用于在计算协方差之前减去列平均值。在代码的第19行,导出了中心向量:

     center <- if (center) 
            colSums(wt * x)
        else 0
    

    在第54行,它被从矩阵中扫出

    x <- sqrt(wt) * sweep(x, 2, center, check.margin = FALSE)
    

    代码的作者正在使用默认值 FUN = "-"

        4
  •  3
  •   dardisco    9 年前

    一个用途是当你在计算的时候 数组的和。在哪里? rowSums colSums 可假定为“权重=1”, sweep

    例如,当按照@James King的例子计算加权协方差矩阵时,就会出现这种情况。

    set.seed(1)
    ## 2x2x2 array
    a1 <- array(as.integer(rnorm(8, 10, 5)), dim=c(2, 2, 2))
    ## 'element-wise' sum of matrices
    ## weights = 1
    rowSums(a1, dims=2)
    ## weights
    w1 <- c(3, 4)
    ## a1[, , 1] * 3;  a1[, , 2] * 4
    a1 <- sweep(a1, MARGIN=3, STATS=w1, FUN="*")
    rowSums(a1, dims=2)
    
        5
  •  0
  •   Ehsan88    10 年前

    你可以用 sweep means sds 在这里是任意的(您可能有一些参考值,希望基于它们标准化数据):

    df=matrix(sample.int(150, size = 100, replace = FALSE),5,5)
    
    df_means=t(apply(df,2,mean))
    df_sds=t(apply(df,2,sd))
    
    df_T=sweep(sweep(df,2,df_means,"-"),2,df_sds,"/")*10+50
    

    > df
         [,1] [,2] [,3] [,4] [,5]
    [1,]  109    8   89   69   15
    [2,]   85   13   25  150   26
    [3,]   30   79   48    1  125
    [4,]   56   74   23  140  100
    [5,]  136  110  112   12   43
    > df_T
             [,1]     [,2]     [,3]     [,4]     [,5]
    [1,] 56.15561 39.03218 57.46965 49.22319 40.28305
    [2,] 50.42946 40.15594 41.31905 60.87539 42.56695
    [3,] 37.30704 54.98946 47.12317 39.44109 63.12203
    [4,] 43.51037 53.86571 40.81435 59.43685 57.93136
    [5,] 62.59752 61.95672 63.27377 41.02349 46.09661