r - I am searching for a minimum in a matrix for every row -
i have matrix in r:
[,1] [,2] [,3]
"7" "8" "0"...
and want have minimum value of every row of matrix without zero. example: in row 1 have values 7,8 , 0. result should 7.
thank in advance!
we can use rowmins
matrixstats
after replace
ing '0' value na
, use na.rm=true
expected minimum value per row.
library(matrixstats) rowmins(replace(m1, m1==0, na), na.rm =true)
or if using base r
apply(m1, 1, fun = function(x) min(x[x!=0]))
or option pmin
after converting data.frame
do.call(pmin, c(as.data.frame(replace(m1, m1==0, na)), na.rm = true))
data
m1 <- matrix(c(7, 4, 5, 8, 3, 0, 0, 1, 4), ncol=3)
Comments
Post a Comment