当我看R软件包的来源时,我看到功能扫描经常使用。 有些时候,如果一个简单的函数在其他时候已经足够(例如“应用”), 就可以使用它,但不可能确切知道它在做什么,而不需要花费相当多的时间来遍历它所在的代码块。如何使用R功能'扫描'
我可以使用更简单的函数重现扫描效果的事实表明我不明白扫描的核心用例,而且这个函数经常使用的事实表明它非常有用。
上下文:
扫是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程序员/分析师在自己的代码中使用扫描。
M-DX不复制您的结果。你是在自问自答。 – John 2010-08-10 00:59:59
我可以计算出这个结果的'apply'的唯一用法就像't(apply(t(M),2, - - ,,dx))',但这很糟糕。 – 2011-05-04 14:32:48