Replace randomly 1000 NA Values in a dataframe column with 0s, without overwriting 1s

Solution 1:

I am assuming that you want to replace 1,000 NA values rather than choosing 1,000 indices and replacing them if they are NA. The following code finds the indices of NA values, then replaces a random sample of 1,000 of those indices with 0.

set.seed(123)
df <- tibble(x = rep(c(1, NA), times = 2000))
indices <- which(is.na(df$x))
df[sample(indices, 1000, replace = FALSE), "x"] <- 0