简体   繁体   中英

Apply function (quantile) to matrix rows and use result to modify row

I have a matrix, A, filled with random values with shape 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?

In the below attempt, I am getting a single value for q, but q should be at least 10 values (one for every row). At that point I should be able to do element-wise division with 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:

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)?

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM