Replace row with NA if any values are non-positive

Viewed 259

My dataset is similar to below:

df <- data.frame(
     A = c(-1, 2, 3),
     B = c(1, 1, -1))
df
#    A  B
# 1 -1  1
# 2  2  1
# 3  3 -1

I am trying to get the following result where values are kept when both A and B are positive:

   A  B
1 NA  NA
2  2  1
3 NA  NA

I have tried df2 <- apply(df, 1:2, function(x) if (x > 0) x else NA), but it returns value when either A or B is positive. How do I add the second condition to it?

2 Answers
Related