随机数列表,只有两个数字

-1 random r

这很简单,但我一直卡住了!我试图创建一个R为1000的零列表,其中数字1.5以随机间隔添加20次,看起来像:

0.000
0.000
1.500
0.000
1.500
0.000
0.000
Run Code Online (Sandbox Code Playgroud)

但我坚持如何将它保持为两个数字?我一直在使用seq命令,但无法正确获取参数...

Rol*_*and 5

对指数进行抽样:

x <- numeric(1000) #adjust to 1020 if you really need 1000 zeros in the result
x[sample(length(x), 20)] <- 1.5
Run Code Online (Sandbox Code Playgroud)