Question about which() with matrices to delete certain stuff

Let's say I have a {100 x 100} matrix (call it "A") and there's 5 Inf in them. My goal is to replace them with NAs.

I can go which(A==Inf) and this might give me something like [1] 4747 4788 ... 5926. How can I use _this_ data to replace the Inf with NAs?

If course I can use which() with sapply(), and iterate over every column of A and get rid of them in 1 line, but that's quite tedious really. I'm looking for something like A[which(A==Inf) ??? ] to replace them.

Example you guys can refer to:


Ambassador to the humans
A[as.matrix(A == Inf)] <- NA
Note that you said you had a matrix but you really have a dataframe - there is a difference so you should be careful with your terminology.