# [R] Loop question?

Berend Hasselman bhh at xs4all.nl
Sat Jan 26 17:05:41 CET 2013

```On 26-01-2013, at 12:31, Andras Farkas <motyocska at yahoo.com> wrote:

>
> Sorry Jeff, probably the new version of Yahoo mail doing the html, I switched back to the older one hope that takes care of the problem. Let me clarify the code below:
>
> TINF <-1
> a <-c(500,750,1000,1250,1500,1750,2000)
> b <-c(8,12,18,24,36,48,60,72,96)
>
> infcprodessa <-function (D, tin, tau, ts)
>     (D * (1 - exp(-0.048 * tin))/(tin * (0.048*79) * (1 - exp(-0.048 * tau)))) * exp(-0.048 * (ts - tin))
>
> z <-sapply(1:1, function(n) infcprodessa(a,TINF,b,b-TINF))
>
> so I am looking to find the combination of a = 1000 and b = 12, which iz a value for "z" that would fall between 15 and 20. Sometimes though there may be more than one combinations that will meet criteria, in that case I would like to select the combination based on the smallest "a" value that has the respective "b" value to meet the criteria,
>

ab <- expand.grid(a=a,b=b)
z <- unlist(sapply(seq_len(nrow(ab)), function(k) infcprodessa(ab[k,1],TINF,ab[k,2],ab[k,2]-TINF)))

z.target <- which(z>=15 & z<=20)
ab[z.target,]
z[z.target]
which.min(ab[z.target,][,"a"])

Berend

```