How to use the 'sweep' function

前端 未结 5 2030
太阳男子
太阳男子 2020-12-02 05:18

When I look at the source of R Packages, i see the function sweep used quite often. Sometimes it\'s used when a simpler function would have sufficed (e.g.,

5条回答
  •  借酒劲吻你
    2020-12-02 05:36

    sweep() can be great for systematically manipulating a large matrix either column by column, or row by row, as shown below:

    > 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
    

    Granted, this example is simple, but changing the STATS and FUN argument, other manipulations are possible.

提交回复
热议问题