替换数据框中的列中的随机值

Gre*_*ory 4 replace r

请告知我如何将列中的一半值替换为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)

我理解为什么会出现错误,但我想强制更换.请告知我该怎么做.

G5W*_*G5W 6

看来你需要

df$x1[sample(nrow(df),250)] <- NA
Run Code Online (Sandbox Code Playgroud)