different If statement

Hi,
I have the following parameters, 1 > y1 > y2 > 0
Im trying to do an if statement to change another parameter ( z) in my r code. i want to do an if statement so when y > y1 then z1 changes to z2 and this remains until y<y2 at which z2 goes back to z1. I cant use a normal if statement because z will change once y falls back below y1 which i dont want.

Does anybody know what type of function i could use to do this?
Thanks

use else in combination with if , and use compound logical conditions i.e. & / |


For more specific help, so as to help us to help you, you might prepare a repr oducible ex ample (reprex) illustrating your issue. Please have a look at this guide, to see how to create one:

1 Like

I have the following code which solves the system of ODEs for some beta and gamma.

RK4 = function(f, a, b, x0, N){
  h = ((b-a)/N)
  time_seq = seq(a, b, h)
  hat_x = matrix(0, ncol = length(x0), nrow = (N+1))
  hat_x[1,] = x0
  for(i in 1:N){
    k1 = h*f(time_seq[i], hat_x[i,])
    k2 = h*f(time_seq[i] + h/2, hat_x[i,] + k1/2)
    k3 = h*f(time_seq[i] + h/2, hat_x[i,] + k2/2)
    k4 = h*f(time_seq[i] + h, hat_x[i,] + k3)
    hat_x[i+1,] = hat_x[i,] + (k1 + 2*k2 + 2*k3 + k4)/6
  }
  return(list(t=time_seq, x=hat_x))
}
   
   
SIR2 = function(beta, gamma, initial, time, N){
  SIR_f = function(t,x){
    f1 = -beta*x[1]*x[2]
    f2 = beta*x[1]*x[2] - gamma*x[2]
    f3 = gamma*x[2]
    return(c(f1,f2,f3))
  }
  return(RK4(SIR_f,0, time, initial, N))
}

it produces a list of answers for f1,f2 and f3 like so

$x
            [,1]        [,2]        [,3]
  [1,] 0.9900000 0.010000000 0.000000000
  [2,] 0.9884796 0.010495766 0.001024608
  [3,] 0.9868866 0.011013539 0.002099887
  [4,] 0.9852179 0.011554028 0.003228073
  [5,] 0.9834706 0.012117937 0.004411473

i want to create a simliar program with more parameters as follows,

lockdown = function(beta, gamma, yin, yout, Lintensity, initial, time, N)

So that when the middle column (x[,2]) > yin, beta changes until x[,2] < yout.

Im not sure in what part of the code to put my conditions and what condition i could put so that the above is achieved.
Thanks for the help

i tried this but receive, error in x[,2] : incorrect number of dimensions

lockdown = function(beta, gamma, yin, yout, Lintensity, initial, time, N){
  SIR_f3 = function(t,x){
    if(x[,2]<yout){
    f1 = -beta*x[1]*x[2]
    f2 = beta*x[1]*x[2] - gamma*x[2]
    f3 = gamma*x[2]
    }else{
    f1= -beta*(1-Lintensity)*x[1]*x[2]
    f2 = beta*(1-Lintensity)*x[1]*x[2] - gamma*x[2]
    f3 = gamma*x[2]
    }
    return(c(f1,f2,f3))
    
  }
  return(RK4(SIR_f3, 0, time, initial, N))
}

I would guess you are looking for ifelse function which is like if and else but vectorised.

yin <- 7
yout <- 9
x <- matrix(1:15, 5, 3)

beta <- ifelse(x[, 2] > yin,
  ifelse(x[, 2] < yout, "In", "Out"), "Neither-Out-Nor_In"
)

still get the same error as before

I wouldnt know where to start with your code, you dont give example params to use to create the object 'x'
did you see though how my code works operating on my object x , in regards to my params yin and yout ?

in general though, if you have a dimension error, then you are misconceptualising the dimensions of the objects you are operating on. i.e. treating a vector as a matrix, or a matrix as a vector, or something of that nature.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.