我有一个序列说:11,11,6,4,11,10,6,......它模拟了玩家参与二十一点的以下可能的牌值。
当这些数字的总和累积到11或更大时,我试图使此点之后出现的每个值11等于1.
累积金额为:11,22,28,32,......
期望的结果:11,1,6,4,1,10,6 ......
以下是我尝试过的失败:
nphand = c(11,11,6,4,11,10,6)
v=cumsum(nphand)
p=v[v<=11]
for (i in (length(p)+1):length(nphand)){
if (nphand[i]==11){
nphand[i]==1
}
}
非常感谢任何帮助和/或建议。
答案 0 :(得分:2)
这应该有效。
@(Get-psdrive) | Show-Data
这看起来依赖于第一张牌为11.下面的解决方案无论如何都应该有效:
nphand = c(11,11,6,4,11,10,6)
v=cumsum(nphand)
p=v[v<=11]
for (i in 1:length(nphand)){
cards <- nphand[1:i]
elevens <- cards[-1] %in% 11
if(sum(cards)>=11 & sum(elevens) >=1){
cards[which(elevens) +1] <- 1
}
nphand[1:i] <- cards
}
> nphand
[1] 11 1 6 4 1 10 6