Equivalent to rowMeans() for min()

前端 未结 6 1447
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-05 10:40

I have seen this question being asked multiple times on the R mailing list, but still could not find a satisfactory answer.

Suppose I a matrix m

6条回答
  •  爱一瞬间的悲伤
    2020-12-05 11:10

    Quite late to the party, but as the author of matrixStats and in case someone spots this, please note that matrixStats::rowMins() is very fast these days, e.g.

    library(microbenchmark)
    library(Biobase)     # rowMin()
    library(matrixStats) # rowMins()
    options(digits=3)
    
    m <- matrix(rnorm(10000000), ncol=10) 
    
    stats <- microbenchmark(
      rowMeans(m), ## A benchmark by OP
      rowMins(m),
      rowMin(m),
      do.call(pmin, as.data.frame(m)),
      apply(m, MARGIN=1L, FUN=min),
      times=10
    )
    
    > stats
    Unit: milliseconds
                                 expr    min     lq   mean median     uq    max
                          rowMeans(m)   77.7   82.7   85.7   84.4   90.3   98.2
                           rowMins(m)   72.9   74.1   88.0   79.0   90.2  147.4
                            rowMin(m)  341.1  347.1  395.9  383.4  395.1  607.7
      do.call(pmin, as.data.frame(m))  326.4  357.0  435.4  401.0  437.6  657.9
     apply(m, MARGIN = 1L, FUN = min) 3761.9 3963.8 4120.6 4109.8 4198.7 4567.4
    

提交回复
热议问题