Skip to content
Prev 303660 / 398503 Next

Row means of matrix.

On 18-08-2012, at 05:28, Yingwei Lee wrote:

            
Define function to compute mean of non zero elements

 f <- function(x) {k <- which(x!=0); mean(x[k])}

then use apply

apply(A,1,f)
[1] 2.5 NaN 1.0

and to remove the NaN you could do

z <- apply(A,1,f)
z[!is.na(z)]

or

z[is.finite(z)]

Berend