I have a complete dataframe. I want to 20% of the values in the dataframe to be replaced by NAs to simulate random missing data.
A <- c(1:10) B <- c(1
You can unlist the data.frame and then take a random sample, then put back in a data.frame.
df <- unlist(df) n <- length(df) * 0.15 df[sample(df, n)] <- NA as.data.frame(matrix(df, ncol=3))
It can be done a bunch of different ways using sample().