samedi 13 janvier 2018

R if function for multiple arguments

I this function

gillespied4 <- function (N, T = 100, dt = 1, ...) 
{
  tt = 0
  n = T%/%dt
  x = N$M
  S = t(N$Post - N$Pre)
  u = nrow(S)
  v = ncol(S)
  xmat = matrix(ncol = u, nrow = n)
  i = 1
  target = 0
  repeat {
    h = N$h(x, tt, ...)
    h0 = sum(h)
    if (h0 < 1e-10) 
      tt = 1e+99
    else tt = tt + rexp(1, h0)
    while (tt >= target) {
      xmat[i, ] = x
      i = i + 1
      target = target + dt
      if ((i*dt) ==c(seq(0,30000,2000))){
        x[4] = x[4]+(10000/15000)
      }
      if (i > n) 
        return(ts(xmat, start = 0, deltat = dt))
    }
    j = sample(v, 1, prob = h)
    x = x + S[, j]
  }
}

, and I am having an issue with the line

  if ((i*dt) ==c(seq(0,30000,2000))){
    x[4] = x[4]+(10000/15000)
  }

And the error I am getting is

In if ((i * dt) == c(seq(0, 30000, 2000))) { ... :
  the condition has length > 1 and only the first element will be used

I know the problem is that the if function doesn't work for multiple values. After searching through stackoverflow most of the answers to this type of question suggest using elseif function or using | but I don't think these apply in this case.

I was wondering if there was any way to overcome this

Aucun commentaire:

Enregistrer un commentaire