请告知我如何将列中的一半值替换为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
Run Code Online (Sandbox Code Playgroud)
我理解为什么会出现错误,但我想强制更换.请告知我该怎么做.