用于替换向量值的 For 循环
A For loop to replace vector value
我不知道我的代码有什么问题。基本上我正在尝试做一个小版本的 Monte Carlo 模拟。
这是我的输入
mydata=[1,4,20,23,37]
prediction=c(0,0,0,0,0,0,0,0,0,0,0,0)
randomvar=runif(12,0,1)
然后我有这个条件:
if i-th value of randomvar
is > 0.97, replace i-th value of prediction
with 5th data from mydata
else if i-th value of randomvar
is > 0.93, replace i-th value of prediction
with 4th data from mydata
else if i-th value of randomvar
is > 0.89, replace i-th value of prediction
with 3rd data from mydata
else if i-th value of randomvar
is > 0.85, replace i-th value of prediction
with 2nd data from mydata
else if i-th value of randomvar
is > 0.81, replace i-th value of prediction
with 1st data from mydata
else 0
所以我写了这个:
mydata=c(1,4,20,23,37)
prediction=c(0,0,0,0,0,0,0,0,0,0,0,0)
random=runif(12,0,1)
for (i in random) {
if (i>0.97) prediction[i]=mydata[5]
else if (i>0.93) prediction[i]=mydata[4]
else if (i>0.89) prediction[i]=mydata[3]
else if (i>0.85) prediction[i]=mydata[2]
else if (i>0.81) prediction[i]=mydata[1]
else prediction[i]=0
}
prediction
结果是
> prediction
[1] 0 0 0 0 0 0 0 0 0 0 0 0
我想知道为什么代码不会根据上述条件更改预测值。谁能帮忙解释一下为什么?反正我是 R 的新手
代码无效,因为使用 runif
生成的随机值不是整数。
> random=runif(12,0,1)
> random
[1] 0.78381826 0.97424261 0.87240491 0.20896106 0.95598721 0.11609102 0.02430861
[8] 0.24213124 0.45808710 0.28205870 0.51469212 0.02672362
现在 prediction[i]
i
在 random
中是没有意义的。
去做 --
for (i in seq_along(random)) {
if (random[i]>0.97) prediction[i]=mydata[5]
else if (random[i]>0.93) prediction[i]=mydata[4]
else if (random[i]>0.89) prediction[i]=mydata[3]
else if (random[i]>0.85) prediction[i]=mydata[2]
else if (random[i]>0.81) prediction[i]=mydata[1]
else prediction[i]=0
}
prediction
> prediction
[1] 0 0 0 0 20 4 0 0 0 0 0 0
我不知道我的代码有什么问题。基本上我正在尝试做一个小版本的 Monte Carlo 模拟。
这是我的输入
mydata=[1,4,20,23,37]
prediction=c(0,0,0,0,0,0,0,0,0,0,0,0)
randomvar=runif(12,0,1)
然后我有这个条件:
if i-th value of
randomvar
is > 0.97, replace i-th value ofprediction
with 5th data frommydata
else if i-th value of
randomvar
is > 0.93, replace i-th value ofprediction
with 4th data frommydata
else if i-th value of
randomvar
is > 0.89, replace i-th value ofprediction
with 3rd data frommydata
else if i-th value of
randomvar
is > 0.85, replace i-th value ofprediction
with 2nd data frommydata
else if i-th value of
randomvar
is > 0.81, replace i-th value ofprediction
with 1st data frommydata
else 0
所以我写了这个:
mydata=c(1,4,20,23,37)
prediction=c(0,0,0,0,0,0,0,0,0,0,0,0)
random=runif(12,0,1)
for (i in random) {
if (i>0.97) prediction[i]=mydata[5]
else if (i>0.93) prediction[i]=mydata[4]
else if (i>0.89) prediction[i]=mydata[3]
else if (i>0.85) prediction[i]=mydata[2]
else if (i>0.81) prediction[i]=mydata[1]
else prediction[i]=0
}
prediction
结果是
> prediction
[1] 0 0 0 0 0 0 0 0 0 0 0 0
我想知道为什么代码不会根据上述条件更改预测值。谁能帮忙解释一下为什么?反正我是 R 的新手
代码无效,因为使用 runif
生成的随机值不是整数。
> random=runif(12,0,1)
> random
[1] 0.78381826 0.97424261 0.87240491 0.20896106 0.95598721 0.11609102 0.02430861
[8] 0.24213124 0.45808710 0.28205870 0.51469212 0.02672362
现在 prediction[i]
i
在 random
中是没有意义的。
去做 --
for (i in seq_along(random)) {
if (random[i]>0.97) prediction[i]=mydata[5]
else if (random[i]>0.93) prediction[i]=mydata[4]
else if (random[i]>0.89) prediction[i]=mydata[3]
else if (random[i]>0.85) prediction[i]=mydata[2]
else if (random[i]>0.81) prediction[i]=mydata[1]
else prediction[i]=0
}
prediction
> prediction
[1] 0 0 0 0 20 4 0 0 0 0 0 0