0

我一直在尝试运行此函数,然后弹出“二进制运算符的非数字参数”。我已经看到了很多与我类似的问题,但我仍然无法弄清楚我的代码有什么问题。

TakeOneIndStep <- function(Itl, N){     # Itl is a vector  
  foo <- ListMaintenance(Itl, N)        # is a vector of same length as Itl. Displays the coordinates of coalesced walks. 
  incrm <- sample(c(-1, 0, 1), size = length(Itl), replace =  T, prob = c(0.25, 0.5, 0.25))
  for (j in 1:length(Itl)){
    if(Itl[j] %in% foo){
     Itl[j] <- (Itl[j] + incrm[j]) %% N
   }else{
     Itl[j] <- "H"              # H is a "placeholder", just to indicate that that particular chain has coalesced and no longer is updated. 
   }
 }
  return(Itl)
}

错误发生在第六行Itl[j] <- (Itl[j] + incrm[j]) %% N

辅助功能代码:

ListMaintenance <- function(Temp, N){    # Temp is a vector
  rez <- CheckCoalescence(Temp)
  fubar <- vector()
  for(i in 1:length(rez)){
    for(x in 0:(N-1)){if (x %in% rez[[i]]){fubar[x] <- min(rez[[i]])}
    }
  } 
  return(fubar)                  # output is a vector. Coordinates with the same value have the index of the smallest occurrence.  
}

CheckCoalescence <- function(Pts){
  mar <- unname(split(seq_along(Pts), Pts))
  return(mar)
}

在大局中,我试图模拟一个具有两个以上不同起点的随机游走过程。因此,参数Itl将是时间 (t-1) 的每次行走的值,并且此函数将递归更新这些值。

出于实际目的,我尝试使用A <- c(0, 2, 3, 2, 6)and测试该功能TakeOneIndStep(A, N = 9)

在这种情况下,A只是一个任意向量。有更多代码可以模拟步行,但我只是介绍了导致错误的部分。

4

1 回答 1

2

问题是Itl[j] <- "H":您通过向向量添加一个字符来更改类。一旦链在您的代码中合并,Itl[j]就不再对数字操作有效。

为了解决这个问题,我更换了

 Itl[j] <- (Itl[j] + incrm[j]) %% N

  Itl[j] <- (as.numeric(Itl[j]) + incrm[j]) %% N
于 2016-07-12T19:46:01.847 回答