Reputation: 73
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