I have a matrix where I want to replace maximum value of each column by -1. How can I do it in R? I tried,
set.seed(14)
mat<- matrix(sample(10,20,replace=TRUE),nr=5)
apply(mat,2,which.max)
[1] 3 2 1 4
I don't know how to replace the matrix. How do I deal with duplicate max value within column? Thanks.
Try:
mat[which(apply(mat, 2, function(x) x == max(x,na.rm=TRUE)))] <- -1
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