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

格雷戈里

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

我知道为什么会显示错误,但我想强制替换。请告知我该怎么做。

G5W

好像你需要

df$x1[sample(nrow(df),250)] <- NA

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章