如何使用“扫一扫"功能 [英] How to use the 'sweep' function

查看:13
本文介绍了如何使用“扫一扫"功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我查看 R 包的源代码时,我看到函数 sweep 经常使用.有时在一个更简单的函数就足够了时使用它(例如,apply),其他时候,没有它就不可能确切地知道它在做什么花费相当多的时间来单步执行它所在的代码块.

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., apply), other times, it's impossible to know exactly what it's is doing without spending a fair amount of time to step through the code block it's in.

我可以使用更简单的函数重现 sweep 的效果这一事实表明我不了解 sweep 的核心用例,而且这个函数经常使用的事实表明它非常有用.

The fact that I can reproduce sweep's effect using a simpler function suggests that i don't understand sweep's core use cases, and the fact that this function is used so often suggests that it's quite useful.

上下文:

sweep 是 R 标准库中的一个函数;它的论点是:

sweep is a function in R's standard library; its arguments are:

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

如您所见,参数与 apply 类似,但 sweep 需要还有一个参数,STATS.

As you can see, the arguments are similar to apply though sweep requires one more parameter, STATS.

另一个关键区别是sweep返回一个与输入数组相同形状的数组,而apply返回的结果取决于传入的函数.

Another key difference is that sweep returns an array of the same shape as the input array, whereas the result returned by apply depends on the function passed in.

sweep 在行动:

# 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

总而言之,我正在寻找的是 sweep 的一两个示例用例.

So in sum, what i'm looking for is an exemplary use case or two for sweep.

请不要背诵或链接到 R 文档、邮件列表或任何主要"R 源——假设我已经阅读了它们.我感兴趣的是有经验的 R 程序员/分析师如何在他们自己的代码中使用 sweep.

Please, do not recite or link to the R Documentation, mailing lists, or any of the 'primary' R sources--assume I've read them. What I'm interested in is how experienced R programmers/analysts use sweep in their own code.

推荐答案

sweep() 通常用于按行或按列操作矩阵时,操作的另一个输入是一个每行/列的不同值.是按行还是按列操作由MARGIN定义,如apply().用于我所谓的其他输入"的值由 STATS 定义.因此,对于每一行(或列),您将从 STATS 中获取一个值并在 FUN 定义的操作中使用.

sweep() is typically used when you operate a matrix by row or by column, and the other input of the operation is a different value for each row / column. Whether you operate by row or column is defined by MARGIN, as for apply(). The values used for what I called "the other input" is defined by STATS. So, for each row (or column), you will take a value from STATS and use in the operation defined by FUN.

例如,如果你想在你定义的矩阵的第一行加 1,第二行加 2,等等,你可以这样做:

For instance, if you want to add 1 to the 1st row, 2 to the 2nd, etc. of the matrix you defined, you will do:

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

坦白说我也没有看懂R文档中的定义,我只是通过查找示例学习的.

I frankly did not understand the definition in the R documentation either, I just learned by looking up examples.

这篇关于如何使用“扫一扫"功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆