[英]Apply function (quantile) to matrix rows and use result to modify row
I have a matrix, A, filled with random values with shape 10x10. 我有一个矩阵A,填充有形状为10x10的随机值。 How can I perform a function on each row (finding the 75th quantile), and divide each element in that row of A by that result?
如何在每行上执行一个函数(查找第75个分位数),并用该结果除以A的那一行中的每个元素?
In the below attempt, I am getting a single value for q, but q should be at least 10 values (one for every row). 在下面的尝试中,我得到q的一个值,但是q至少应为10个值(每行一个)。 At that point I should be able to do element-wise division with
A/q
. 在这一点上,我应该能够对
A/q
逐元素除法。 What am I doing wrong? 我究竟做错了什么?
A <- matrix(rnorm(10 * 10), 10, 10)
q <- c(quantile(A[1,], 0.75))
A/q
There's rowQuantiles
from the matrixStats
package: 有
rowQuantiles
从matrixStats
包:
library(matrixStats)
res <- A / rowQuantiles(A, probs=0.75)
Same result? 结果一样吗?
identical(apply(A, 1, quantile, probs=0.75), rowQuantiles(A, probs=0.75))
[1] TRUE
Is it faster? 它更快吗?
library(microbenchmark)
microbenchmark(apply=apply(A, 1, quantile, probs=0.75),
matStat=rowQuantiles(A, probs=0.75))
Unit: microseconds
expr min lq mean median uq max neval cld
apply 788.298 808.9675 959.816 829.3515 855.154 13259.652 100 b
matStat 246.611 267.2800 278.302 276.1180 284.386 362.075 100 a
On this matrix, definitely. 在这个矩阵上,绝对可以。
What about on a bigger matrix (1000 X 1000)? 在更大的矩阵(1000 X 1000)上呢?
A <- matrix(rnorm(1e6), 1000, 1000)
microbenchmark(apply=apply(A, 1, quantile, probs=0.75),
matStat=rowQuantiles(A, probs=0.75))
Unit: milliseconds
expr min lq mean median uq max neval cld
apply 115.57328 123.4831 183.1455 139.82021 308.3715 353.1725 100 b
matStat 74.22657 89.2162 136.1508 95.41482 113.0969 745.1526 100 a
Not as dramatic, but still yes (ignoring the max value). 不那么引人注目,但仍然可以(忽略最大值)。
Solved the issue by using apply
, as below: 通过使用
apply
解决了该问题,如下所示:
A <- matrix(rnorm(10 * 10), 10, 10)
q <- apply(A, 1, quantile, probs = c(0.75), na.rm = TRUE)
A <- A/q
It technically answers the question, but a vectorized approach would be nice. 从技术上讲,它回答了这个问题,但是矢量化方法会很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.