Rewarp
Rewarp

Reputation: 220

Stop a looping function when one value is greater than another within loop

I have been trying to write a while command to stop the looping function when one value generated by the loop exceeds the other. However, I have failed to figure out the proper way to do it.

The for loop runs for 30 days, but I want it to stop as soon as the last value of parasite_l.A is less than than parasite_l.B.

I have included the working code I have for generating the data and the for loop.

Alternative solutions without a limit on the loop would also be greatly appreciated.

# Subject A, initially 400 parasites, growing by 10 %
subA = 400
infA = 1.1

# Subject B, initially 120 parasites, growing by 20 %
subB = 120
infB = 1.2

# How many days to model
days = 30

days_seq = seq(1, days, 1)

# Parasite load for A
parasite_l.A = rep(NA, days)
parasite_l.A[1] = subA

# Parasite load for B
parasite_l.B = rep(NA, days)
parasite_l.B[1] = subB

# Loop for subject A and B
for(i in 1:(days)){
    parasite_l.A[i+1] = parasite_l.A[i]*(infA)
    parasite_l.B[i+1] = parasite_l.B[i]*(infB)
  }

parasite_l.A
parasite_l.B

Upvotes: 3

Views: 2136

Answers (4)

chrismillah
chrismillah

Reputation: 3914

if (parasite_l.A < parasite_l.B) { // if parasite a is less than b, do the following // 
    for(i in 1:(days)){
       parasite_l.A[i+1] = parasite_l.A[i]*(infA)
       parasite_l.B[i+1] = parasite_l.B[i]*(infB)
    }
}

Upvotes: 1

Pierre L
Pierre L

Reputation: 28441

There is a built-in control flow function for what you are referring to named while. As long as the conditions are met it will continue to loop.

i <- 1    
while (parasite_l.A[i] > parasite_l.B[i]) {
      parasite_l.A[i+1] = parasite_l.A[i]*(infA)
    parasite_l.B[i+1] = parasite_l.B[i]*(infB)
    i <- i + 1
}

# parasite_l.A
#  [1]  400.0000  440.0000  484.0000  532.4000  585.6400  644.2040  708.6244
#  [8]  779.4868  857.4355  943.1791 1037.4970 1141.2467 1255.3714 1380.9085
# [15] 1518.9993        NA        NA        NA        NA        NA        NA
# [22]        NA        NA        NA        NA        NA        NA        NA
# [29]        NA        NA
#  parasite_l.B
#  [1]  120.0000  144.0000  172.8000  207.3600  248.8320  298.5984  358.3181
#  [8]  429.9817  515.9780  619.1736  743.0084  891.6100 1069.9321 1283.9185
# [15] 1540.7022        NA        NA        NA        NA        NA        NA
# [22]        NA        NA        NA        NA        NA        NA        NA
# [29]        NA        NA

Upvotes: 3

Paul James
Paul James

Reputation: 530

Use an index value (i), a couple of counters (A.index.value, B.index.value), and a while loop:

# Subject A, initially 400 parasites, growing by 10 %
subA <- A.index.value <- 400
infA <- 1.1

# Subject B, initially 120 parasites, growing by 20 %
subB <- B.index.value <- 120
infB <- 1.2

# How many days to model
days <- 30

days_seq <- seq(1, days, 1)

# Parasite load for A
parasite_l.A <- rep(NA, days)
parasite_l.A[1] <- subA

# Parasite load for B
parasite_l.B <- rep(NA, days)
parasite_l.B[1] <- subB

# While Loop for subject A and B
i <- 1
while (A.index.value > B.index.value) {
    parasite_l.A[i+1] <- A.index.value <- parasite_l.A[i]*(infA)
    parasite_l.B[i+1] <- B.index.value <-  parasite_l.B[i]*(infB)
    i <- i + 1
}

parasite_l.A
parasite_l.B

With the results being:

> parasite_l.A
 [1]  400.00  440.00  484.00  532.40  585.64  644.20  708.62  779.49  857.44 943.18 1037.50
[12] 1141.25 1255.37 1380.91 1519.00      NA      NA      NA      NA      NA      NA      NA
[23]      NA      NA      NA      NA      NA      NA      NA      NA
> parasite_l.B
 [1]  120.00  144.00  172.80  207.36  248.83  298.60  358.32  429.98  515.98  619.17  743.01
[12]  891.61 1069.93 1283.92 1540.70      NA      NA      NA      NA      NA      NA      NA
[23]      NA      NA      NA      NA      NA      NA      NA      NA
> 

Upvotes: 2

PereG
PereG

Reputation: 1846

Use inside the loop something like:

    if (parasite_l.A > parasite_l.B) {
      break
}

Upvotes: 0

Related Questions