How do I add random `NA`s into a data frame
问题 I created a data frame with random values n <- 50 df <- data.frame(id = seq (1:n), age = sample(c(20:90), n, rep = TRUE), sex = sample(c("m", "f"), n, rep = TRUE, prob = c(0.55, 0.45)) ) and would like to introduce a few NA values to simulate real world data. I am trying to use apply but cannot get there. The line apply(subset(df,select=-id), 2, function(x) {x[sample(c(1:n),floor(n/10))]}) will retrieve random values alright, but apply(subset(df,select=-id), 2, function(x) {x[sample(c(1:n)