基于另一个值

时间:2017-11-20 00:45:23

标签: r loops dataframe

Test3 <- function(n,d) {

  die1 = sample(1:d, n, replace = TRUE)
  die2 = sample(1:d, n, replace = TRUE)
  move = die1 + die2
  position_before_roll = move
  position_after_roll = move
  jail = move


  monopoly_sim = data.frame(position_before_roll,die1,die2,move,position_after_roll,jail)
  monopoly_sim[1,1]=0

  i = 1
for(i in 1:n){
  #Needs to be position after roll + Dice Roll (Move)

  monopoly_sim[i,5] = monopoly_sim[i,1] + monopoly_sim[i,4]
  monopoly_sim[i+1,1] = monopoly_sim[i,5] 
}
i=1
  for(i in 1:n) {

    if (monopoly_sim[i,1] + monopoly_sim[1,4] >= 39) {
      monopoly_sim[i,5] =  0 + monopoly_sim[i,1] + monopoly_sim[i,4] - 39
      monopoly_sim[i+1, 1] = monopoly_sim[i,5]
      }  

    else {
    monopoly_sim[i,5] = monopoly_sim[i,1] + monopoly_sim[i,4]
    monopoly_sim[i+1, 1] = monopoly_sim[i,5]
    }
  }

  i=1
    for (i in 1:n) {
      if (monopoly_sim[i,2] == monopoly_sim[i,3] && monopoly_sim[i+1,2] == monopoly_sim[i+1,3] 
      && monopoly_sim[i+2,2] == monopoly_sim[i+2,3] && monopoly_sim[i+3,2] != monopoly_sim[i+3,3])
      {monopoly_sim[i+2,6] = "Yes" }  
      else {monopoly_sim[i+2,6] = "No"}
         }

  return(monopoly_sim)

  }

df = Test3(200,6)
x1= as.data.frame(df)

这是我的代码。该函数的目标是创建数据框并相应地填充值。基于开始位置为0然后添加骰子卷1和骰子卷2。

我已将position_before_roll设为相等(上一行的position_after_roll)。然而,我正在努力的地方是我希望我的“董事会”有39个职位,所以在它将进入一个大于39的位置后,它会绕回来。为了尝试这样做,我将它设置为大于39后减去39。

当我看到我的输出DF时,虽然它并不总是这样做。相反,它减去有时小于39的值,从而产生负数。

1 个答案:

答案 0 :(得分:0)

我挖掘了你的代码,就你看来,你正试图模拟大富翁游戏。下一次,请提供一些上下文并说明您要对代码执行的操作,尤其是在您尝试实施的情况下。 我更改了所有变量引用,因此代码变得更易读和友好。我还删除了一个循环并在第一个循环中插入了i > 39的检查。一些变量作为NA启动,并且它们是在移动中计算的。尽管如此,这些只是简单的改进,还有很多工作要做,以使其完美。请看一下:

monopoly_sim <- function(n,d) {

#n <- 200
#d <- 6

die1 = sample(1:d, n, replace = TRUE)
die2 = sample(1:d, n, replace = TRUE)
move = die1 + die2


df = data.frame(position_before_roll = NA, die1, die2, move, position_after_roll = NA, jail = "No", stringsAsFactors = F)
df$position_before_roll[1] = 0

for(i in 1:n){
  # Simulate rolling dice and moving 
  df$position_after_roll[i] = df$position_before_roll[i] + df$move[i]

  # If position after roll greater than 38, substract 39 from it
  if (df$position_after_roll[i] >=39) {df$position_after_roll[i] <- df$position_after_roll[i] - 39}

  # Calculate start position on the next move
  if (i < n ) {df$position_before_roll[i+1] = df$position_after_roll[i]}


}

# Check if move should lead to the jail
for (i in 1:n-3) {
if (df$die1[i] == df$die2[i] &&
    df$die1[i+1] == df$die2[i+1] &&
    df$die1[i+2] == df$die2[i+2] &&
    df$die1[i+3] != df$die2[i+3])
  {df$jail[i+2] = "Yes" }  
  else {df$jail[i+2] = "No"}
}
return(df)
}


df <- monopoly_sim(200,6)

head(df, 40)

   position_before_roll die1 die2 move position_after_roll jail
1                     0    6    4   10                  10   No
2                    10    2    4    6                  16   No
3                    16    5    6   11                  27   No
4                    27    4    2    6                  33   No
5                    33    2    5    7                   1   No
6                     1    5    3    8                   9   No
7                     9    3    6    9                  18   No
8                    18    3    6    9                  27   No
9                    27    5    6   11                  38   No
10                   38    5    6   11                  10   No
11                   10    5    3    8                  18   No
12                   18    6    2    8                  26   No
13                   26    3    3    6                  32   No
14                   32    5    1    6                  38   No
15                   38    1    4    5                   4   No
16                    4    3    6    9                  13   No
17                   13    6    6   12                  25   No
18                   25    3    5    8                  33   No
19                   33    5    5   10                   4   No
20                    4    1    6    7                  11   No
21                   11    3    6    9                  20   No
22                   20    1    4    5                  25   No
23                   25    4    6   10                  35   No
24                   35    4    4    8                   4   No
25                    4    5    4    9                  13   No
26                   13    1    1    2                  15   No
27                   15    1    6    7                  22   No
28                   22    6    1    7                  29   No
29                   29    1    2    3                  32   No
30                   32    5    3    8                   1   No
31                    1    6    6   12                  13   No
32                   13    6    6   12                  25   No
33                   25    4    1    5                  30   No
34                   30    1    6    7                  37   No
35                   37    6    4   10                   8   No
36                    8    3    4    7                  15   No
37                   15    4    1    5                  20   No
38                   20    5    1    6                  26   No
39                   26    5    5   10                  36   No
40                   36    3    6    9                   6   No