Equivalent to rowMeans() for min()

前端 未结 6 1449
爱一瞬间的悲伤
爱一瞬间的悲伤 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:27

    You could use pmin, but you would have to get each column of your matrix into a separate vector. One way to do that is to convert it to a data.frame then call pmin via do.call (since data.frames are lists).

    system.time(do.call(pmin, as.data.frame(m)))
    #    user  system elapsed 
    #   0.940   0.000   0.949 
    system.time(apply(m,1,min))
    #    user  system elapsed 
    #   16.84    0.00   16.95 
    

提交回复
热议问题