R 练习循环

R 练习循环,r,for-loop,R,For Loop,我正在努力提高我的函数写作技巧,但我对函数的正确结构有点困惑。我找了很多例子,但没有一个对我来说那么清楚。我的目标是在for循环中运行#run over部分,并构建一个函数,允许我控制循环次数 现在,我要说的是: set.seed(123) #Start but setting the conditions and being the Win Lose counters Count_Win_Hunt=0 Count_Win_Moose=0 #RUN over and over Hunte

我正在努力提高我的函数写作技巧,但我对函数的正确结构有点困惑。我找了很多例子,但没有一个对我来说那么清楚。我的目标是在
for
循环中运行
#run over
部分,并构建一个函数,允许我控制循环次数

现在,我要说的是:

set.seed(123)
#Start but setting the conditions and being the Win Lose counters 
Count_Win_Hunt=0 
Count_Win_Moose=0 

#RUN over and over
Hunter=1
Moose=7
win=0

while(win != 1){ a = sample(1:6, 1) # dice roll 
if( a<= 4) {Moose = Moose+a} else{Hunter = Hunter+a}
if( Hunter >= Moose  ) { Count_Win_Hunt = Count_Win_Hunt +1 } else if( Moose >= 12) {Count_Win_Moose = Count_Win_Moose + 1}

if( Hunter >= Moose || Moose >= 12 ) {win = win+1} else {
  #if not condition not meet roll again
  a = sample(1:6, 1) # dice roll
  if( a<= 4) {Moose = Moose+a} else{ Hunter = Hunter+a}}}


# calculated the average win rates 
paste0( round(Count_Win_Hunt/(Count_Win_Hunt+Count_Win_Moose),4)*100,"%"," of the time the Hunter won")
paste0( round(Count_Win_Moose/(Count_Win_Hunt+Count_Win_Moose),4)*100,"%"," of the time the Moose won")
set.seed(123)
#开始,但设置条件并成为赢家-输家计数器
Count\u Win\u Hunt=0
Count\u Win\u Moose=0
#反复
亨特=1
驼鹿=7
赢=0
而(赢!=1){a=sample(1:6,1)#掷骰子
如果(a=Moose){Count\u Win\u Hunt=Count\u Win\u Hunt+1}如果(Moose>=12){Count\u Win\u Moose=Count\u Win\u Moose+1}
如果(猎人>=Moose | | Moose>=12){win=win+1}其他{
#如果不符合条件,则再次滚动
a=样品(1:6,1)#骰子卷

如果(a除了我对您的问题的一般问题(请更具体地说明您的实际问题)之外,您的for循环的语法错误。它们应该是这样的:

if (val <= 4) {
probability + (hunter,goose+val,num+1)
}
else {
probability + (hunter+val,goose,num+1)
}
dice_roll <- function(hunter,goose, win){
# While to check for winning condition
while(win != 1){
 dice_roll = sample(1:6, 1) # simulate dice roll

 # If statement depending on dice roll, increasing value of hunter or goose by dice roll

 # Change win condition
 If(hunter >= goose){
  win <- 1
 }
}

dice_roll(1,7,0)
for(val顺序)
{
陈述
}
因此,应用于函数时,它们应该如下所示:

if (val <= 4) {
probability + (hunter,goose+val,num+1)
}
else {
probability + (hunter+val,goose,num+1)
}
dice_roll <- function(hunter,goose, win){
# While to check for winning condition
while(win != 1){
 dice_roll = sample(1:6, 1) # simulate dice roll

 # If statement depending on dice roll, increasing value of hunter or goose by dice roll

 # Change win condition
 If(hunter >= goose){
  win <- 1
 }
}

dice_roll(1,7,0)
for(c(1:4)中的val)
{
概率+(猎人,鹅+val,数量+1)
}
用于(c(5:6)中的val)
{
概率+(猎人,鹅+val,数量+1)
打印概率
}
然而,它们不仅在语法上是错误的,而且它们的内容似乎也是错误的

例如,在第二个for循环中,鹅向前走,即使它应该是猎人。此外,这不是两个for循环,而是一个if语句,如下所示:

if (val <= 4) {
probability + (hunter,goose+val,num+1)
}
else {
probability + (hunter+val,goose,num+1)
}
dice_roll <- function(hunter,goose, win){
# While to check for winning condition
while(win != 1){
 dice_roll = sample(1:6, 1) # simulate dice roll

 # If statement depending on dice roll, increasing value of hunter or goose by dice roll

 # Change win condition
 If(hunter >= goose){
  win <- 1
 }
}

dice_roll(1,7,0)

if(val)你的具体问题是什么?什么有效,什么无效?你的代码是否完整,是否需要检查(那么代码检查SE可能更合适)或者您有可以共享的错误吗?因此我认为我已经有了正确的系统来计算百分比,并且我已经在纸上完成了。我认为前两个
if
语句可以工作。我在函数中运行
for
循环时遇到了问题。我得到了以下错误:
错误:意外的“+”:“对于(c(1:4)中的val)概率+函数(hunter,goose+”
。我从来没有听说过code review SE,所以我来检查一下。您正在向浮点添加一个列表,这根本不起作用(因此意外的
++
,因为您无法计算这些变量)。请看我的整体结构改进建议。干杯,这很有意义,我会再尝试一次。因此,我认为我已经做到了。
while
函数真的很有帮助,所以感谢@Fnguyen。
set.seed(123)#设置条件计数(u Win)Hunt=0计数(u Win)Moose=0猎人=1驼鹿=7赢=0,而(Win!=1){a=sample(1:6,1)如果(a=Moose){Count\u-Win\u-Hunt=Count\u-Win\u-Hunt+1}如果(Moose>=12){Count\u-Win\u-Moose=Count\u-Win\u-Moose+1}如果(Hunter>=Moose\124; muose>=12){Win Win Win+1}如果条件不满足,则再次掷骰子(1:6,1){( a@pr1g11很乐意提供帮助!但是请记住,SO的目标是在将来帮助所有人回答问题,因此不鼓励更改初始问题。我建议重新插入原始的“错误代码”"只需在文章末尾添加新代码作为更新。这样,未来寻找答案的用户就会知道他们是否有类似问题。这很有意义,我将重述我的原始代码。有没有办法将我的新代码作为循环运行?模拟100个游戏?谢谢!@pr1g11我会将您当前的代码打包到函数a中使用
replicate(100,myfunction)