gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:26:31 25 4
gpt4 key购买 nike

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

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

最佳答案

看来你需要

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

关于替换数据框中列中的随机值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44459514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com