将函数应用于矩阵或数据框的每一行
假设我有一个 2 矩阵和一个将 2 向量作为其参数之一的函数。我想将该函数应用于矩阵的每一行并获得一个 n 向量。如何在 R 中做到这一点?
例如,我想计算三个点上的二维标准正态分布的密度:
bivariate.density(x = c(0, 0), mu = c(0, 0), sigma = c(1, 1), rho = 0){
exp(-1/(2*(1-rho^2))*(x[1]^2/sigma[1]^2+x[2]^2/sigma[2]^2-2*rho*x[1]*x[2]/(sigma[1]*sigma[2]))) * 1/(2*pi*sigma[1]*sigma[2]*sqrt(1-rho^2))
}
out <- rbind(c(1, 2), c(3, 4), c(5, 6))
如何将函数应用于 out
的每一行?
如何以您指定的方式将除点之外的其他参数的值传递给函数?
Suppose I have a n by 2 matrix and a function that takes a 2-vector as one of its arguments. I would like to apply the function to each row of the matrix and get a n-vector. How to do this in R?
For example, I would like to compute the density of a 2D standard Normal distribution on three points:
bivariate.density(x = c(0, 0), mu = c(0, 0), sigma = c(1, 1), rho = 0){
exp(-1/(2*(1-rho^2))*(x[1]^2/sigma[1]^2+x[2]^2/sigma[2]^2-2*rho*x[1]*x[2]/(sigma[1]*sigma[2]))) * 1/(2*pi*sigma[1]*sigma[2]*sqrt(1-rho^2))
}
out <- rbind(c(1, 2), c(3, 4), c(5, 6))
How to apply the function to each row of out
?
How to pass values for the other arguments besides the points to the function in the way you specify?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您只需使用 apply() 函数:
这需要一个矩阵并对每一行应用一个(愚蠢的)函数。您将额外的参数传递给函数,作为
apply()
的第四个、第五个……参数。You simply use the
apply()
function:This takes a matrix and applies a (silly) function to each row. You pass extra arguments to the function as fourth, fifth, ... arguments to
apply()
.如果您想应用 sum 或 Mean 等常见函数,则应使用 rowSums 或 rowMeans ,因为它们比 apply(data, 1, sum ) 方法。否则,请坚持使用
apply(data, 1, fun)
。您可以在 FUN 参数之后传递其他参数(正如 Dirk 已经建议的那样):然后您可以执行以下操作:
In case you want to apply common functions such as sum or mean, you should use
rowSums
orrowMeans
since they're faster thanapply(data, 1, sum)
approach. Otherwise, stick withapply(data, 1, fun)
. You can pass additional arguments after FUN argument (as Dirk already suggested):Then you can do something like this:
这是将函数应用于矩阵每一行的简短示例。
(这里,应用的函数将每一行标准化为 1。)
注意:
apply()
的结果必须使用转置 >t()
获得与输入矩阵A
相同的布局。结果:
Here is a short example of applying a function to each row of a matrix.
(Here, the function applied normalizes every row to 1.)
Note: The result from the
apply()
had to be transposed usingt()
to get the same layout as the input matrixA
.Result:
Apply 的工作做得很好,但速度相当慢。
使用 sapply 和 vapply 可能很有用。 dplyr 的 rowwise 也很有用
让我们看一个如何对任何数据框进行行乘积的示例。
请注意,在使用 vapply/sapply/apply 之前分配给变量是很好的做法,因为它可以大大减少时间。让我们看看微基准测试结果
仔细看看 t() 是如何使用的
Apply does the job well, but is quite slow.
Using sapply and vapply could be useful. dplyr's rowwise could also be useful
Let's see an example of how to do row wise product of any data frame.
Note that assigning to variable before using vapply/sapply/ apply is good practice as it reduces time a lot. Let's see microbenchmark results
Have a careful look at how t() is being used
第一步是创建函数对象,然后应用它。如果您想要一个具有相同行数的矩阵对象,您可以预定义它并使用 object[] 形式,如图所示(否则返回值将被简化为向量):
如果您想使用默认参数以外的其他参数那么调用应该在函数后面包含命名参数:
apply() 也可以用于更高维的数组,并且 MARGIN 参数可以是向量也可以是单个整数。
First step would be making the function object, then applying it. If you want a matrix object that has the same number of rows, you can predefine it and use the object[] form as illustrated (otherwise the returned value will be simplified to a vector):
If you wanted to use other than your default parameters then the call should include named arguments after the function:
apply() can also be used on higher dimensional arrays and the MARGIN argument can be a vector as well as a single integer.
如果您想使用数据集的不同部分而不是单个值,另一种方法是使用 rollapply(data, width, FUN, ...)。使用宽度向量允许您在数据集的不同窗口上应用函数。我用它来构建一个自适应过滤例程,尽管它不是很有效。
Another approach if you want to use a varying portion of the dataset instead of a single value is to use
rollapply(data, width, FUN, ...)
. Using a vector of widths allows you to apply a function on a varying window of the dataset. I've used this to build an adaptive filtering routine, though it isn't very efficient.使用
across
、rowSums
和rowMeans
的 dplyr 方法。A dplyr Approach using
across
,rowSums
androwMeans
.