4

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.

1 Answers1

7

Try:

 mat[which(apply(mat, 2, function(x) x == max(x,na.rm=TRUE)))] <- -1
akrun
  • 874,273
  • 37
  • 540
  • 662