Reputation: 119
Please advise how can I replace half of values in a column to NA:
# Generate 500 values with a skewed distribution
x1 <- round(rbeta(500,0.5,3)*100,0)
# Assign variable to a data frame
df <- data.frame(x1)
# Replace 250 random values in a column 'x1' to NA
df[sample(x1,250)] <- NA
The following mistake is shown:
Error in `[<-.data.frame`(`*tmp*`, sample(x1, 250), value = NA) :
new columns would leave holes after existing columns
I understand why the mistake is shown, but I would like to force the replacement. Please advise on how can I do that.
Upvotes: 9
Views: 2362