New_to_ML
New_to_ML

Reputation: 73

Is there any way we can replace a value in Matrix with NA using apply function only

I want to replace wherever value is greater than 8 to NA using only "Apply" function in a matrix of 1 to 20

m <- matrix(c(1:10, 11:20), nrow = 5, ncol = 4)

I have tried apply(m, 1:2, function(x) x = replace_na(x, 0)) after making values zero (whichever was greater than 8) Second I tried is

apply(m, 1:2, function(x) is.na(x) <- !x)

Third I tried is

apply(m, 1:2, function(x) replace_na(x>8))

Matrix should have all the values as NA from 9 to 20

Upvotes: 2

Views: 58

Answers (1)

akrun
akrun

Reputation: 887088

We can assign with is.na

`is.na<-`(m, m > 8)

Upvotes: 4

Related Questions