R: how can I replace specific entries of a matrix?

Viewed 19

Let

m <- 3*matrix(1:12, 3, 4)

using the which() function I then can access all entries that are above a given threshold like so

which(m > 25) which returns a list of integer entry indices, or

which(m > 25, arr.ind = TRUE) returning more matrix-like indices.

However I cannot use something like m[which(m > 25)] in order to change those values (say to 100). I would greatly appreciate any pointers :)

1 Answers

Try:

m[m>25] <- 100
m
     [,1] [,2] [,3] [,4]
[1,]    3   12   21  100
[2,]    6   15   24  100
[3,]    9   18  100  100
Related