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 replaceing '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

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -