I have a matrix of size 5000*5000, with 90% values as 0. Is there a ready-made solution available to calculate the mean, median for this matrix after excluding '0' ?
One crude solution is change all 0 to NA and use
median(x, na.rm=TRUE)
Another solution is to manually scan the matrix and create another vector containing values and then figure out mean, median etc.
Any other better alternative ?
If you want median and mean of overall matrix then try following
median(x[x>0])
mean(x[x>0])
If you want median and mean row wise
apply(x,1,function(x){mean(x[x>0])})
apply(x,1,function(x){median(x[x>0])})
If you want median and mean coloumn wise
apply(x,2,function(x){mean(x[x>0])})
apply(x,2,function(x){median(x[x>0])})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With