Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace maximum value of each column

Tags:

r

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.

like image 510
user3736918 Avatar asked Mar 19 '23 08:03

user3736918


1 Answers

Try:

 mat[which(apply(mat, 2, function(x) x == max(x,na.rm=TRUE)))] <- -1
like image 174
akrun Avatar answered Apr 02 '23 02:04

akrun