I want to set NA in each element of the matrix, where the value in the column is greater than or equal to the value of this vector. For example, I can create a matrix:
set.seed(1)
zz <- matrix(data = round(10L * runif(12)), nrow = 4, ncol = 3)
what gives for zz:
[,1] [,2] [,3]
[1,] 8 5 7
[2,] 6 5 1
[3,] 5 10 3
[4,] 9 1 9
and for the comparison vector (for example):
xx <- round(10L * runif(4))
where xx:
[1] 6 3 8 2
if I perform this operation:
apply(zz,2,function(x) x >= xx)
I get:
[,1] [,2] [,3]
[1,] TRUE FALSE TRUE
[2,] TRUE TRUE FALSE
[3,] FALSE TRUE FALSE
[4,] TRUE FALSE TRUE
I want wherever I have a TRUE element, I want NA and wherever I have FALSE. I get a number in the zz matrix (e.g. manually ...):
NA 5 NA
NA NA 1
5 NA 3
NA 1 NA
I can combine some "for" loops to do what I want, but is there a vector based way?
Thanks for any advice.