To replace all 0 values to NA in the data frame you can use the following syntax:
set.seed(123)
df <- data.frame(x = sample(0:2, 5, TRUE), y = sample(0:1, 5, TRUE))
df
x y
1 2 1
2 2 1
3 2 1
4 1 0
5 2 0
df[df == 0] <- NA
df
df
x y
1 2 1
2 2 1
3 2 1
4 1 0
5 2 0
You can also use the na_if function from the dplyr package, as follows:
library('dplyr')
na_if(df, 0)
x y
1 2 1
2 2 1
3 2 1
4 1 NA
5 2 NA