Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我刚开始学习 R,我的任务是创建一个包含 10000 个值的向量,其正态分布,mean = 0和sd = 100. 我做到了。
mean = 0
sd = 100
x <- rnorm(10000, mean = 0, sd = 100)
但现在我被要求values between 500 and 700在该向量中引入 1000 个随机位置。
values between 500 and 700
谁能帮我?
如果您打算将x向量 中的 1000 个元素替换为 500 到 700 之间的值,则首先需要生成这 1000 个元素:
x
r <- runif(1000, min=500, max=700)
我在这里假设随机值统一在 500 到 700 之间。
然后您需要选择将这些值放入的位置:
idx <- sample(10000, 1000)
最后,替换这些地方的值:
x[ idx ] <- r
最后,查看您的操作的结果:
hist(x)
它应该看起来像: