Dear All I have the following data (somewhat simplyfied): TINF <-1 a <-c(500,750,1000,1250,1500,1750,2000) b <-c(8,12,18,24,36,48,60,72,96) following function: 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(1000,TINF,12,12-TINF)) is there a way to select the combination of respective a and b values that would result in a calculated z that is between 15 and 20? In this case the a would be 1000 and the b would be 12 (other combinations are also possible), but how could I automatically find them? perhaps a loop? Apreciate the help, Sincerely, Andras [[alternative HTML version deleted]]
Please read the Posting Guide no html email reproducible example please In general, you can use expand.grid to generate all combinations of inputs, compute results as a vector just as long as the expand.grid data frame has rows, and identify which results meet your criteria by a logical test, and use that test to identify which input combinations worked. Provide a working starting point and someone might give you working code as an answer. (where do a and b come into your problem?) --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Andras Farkas <motyocska at yahoo.com> wrote:>Dear All >? >I have the following data (somewhat simplyfied): >? >TINF <-1 >a <-c(500,750,1000,1250,1500,1750,2000) >b <-c(8,12,18,24,36,48,60,72,96) >? >following function: >? >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(1000,TINF,12,12-TINF)) >? >is there a way to select the combination of respective a and b values >that would result in a calculated z that is between 15 and 20? In this >case the a would be 1000 and the b would be 12 (other combinations are >also possible), but how could I automatically find them? perhaps a >loop? >? >Apreciate the help, >? >Sincerely, >? >Andras > [[alternative HTML version deleted]] > > > >------------------------------------------------------------------------ > >______________________________________________ >R-help at r-project.org mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.
Here's a toy example which you can apply the logic of: dfr <- expand.grid(1:3,1:2) results <- apply(dfr, 1, sum) dfr[results==4,] On 25 January 2013 22:19, Andras Farkas <motyocska at yahoo.com> wrote:> > Dear All > > I have the following data (somewhat simplyfied): > > TINF <-1 > a <-c(500,750,1000,1250,1500,1750,2000) > b <-c(8,12,18,24,36,48,60,72,96) > > following function: > > 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(1000,TINF,12,12-TINF)) > > is there a way to select the combination of respective a and b values that would result in a calculated z that is between 15 and 20? In this case the a would be 1000 and the b would be 12 (other combinations are also possible), but how could I automatically find them? perhaps a loop? > > Apreciate the help, > > Sincerely, > > Andras > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
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, sorry for the confusion, thanks, Andras --- On Sat, 1/26/13, Jeff Newmiller <jdnewmil at dcn.davis.CA.us> wrote:> From: Jeff Newmiller <jdnewmil at dcn.davis.CA.us> > Subject: Re: [R] Loop question? > To: "Andras Farkas" <motyocska at yahoo.com>, "r-help at r-project.org" <r-help at r-project.org> > Date: Saturday, January 26, 2013, 2:09 AM > Please read the Posting Guide > no html email > reproducible example please > > In general, you can use expand.grid to generate all > combinations of inputs, compute results as a vector just as > long as the expand.grid data frame has rows, and identify > which results meet your criteria by a logical test, and use > that test to identify which input combinations worked. > Provide a working starting point and someone might give you > working code as an answer. (where do a and b come into your > problem?) > --------------------------------------------------------------------------- > Jeff Newmiller? ? ? ? ? ? > ? ? ? ? ? ? The? > ???.....? ? > ???.....? Go Live... > DCN:<jdnewmil at dcn.davis.ca.us>? > ? ? ? Basics: ##.#.? ? > ???##.#.? Live Go... > ? ? ? ? ? ? ? ? > ? ? ? ? ? ? ? ? > ? ? ? Live:???OO#.. Dead: > OO#..? Playing > Research Engineer (Solar/Batteries? ? ? > ? ? ? O.O#.? ? > ???#.O#.? with > /Software/Embedded Controllers)? ? ? ? > ? ? ???.OO#.? ? > ???.OO#.? rocks...1k > --------------------------------------------------------------------------- > > Sent from my phone. Please excuse my brevity. > > Andras Farkas <motyocska at yahoo.com> > wrote: > > >Dear All > >? > >I have the following data (somewhat simplyfied): > >? > >TINF <-1 > >a <-c(500,750,1000,1250,1500,1750,2000) > >b <-c(8,12,18,24,36,48,60,72,96) > >? > >following function: > >? > >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(1000,TINF,12,12-TINF)) > >? > >is there a way to select the combination of respective a > and b values > >that would result in a calculated z that is between 15 > and 20? In this > >case the a would be 1000 and the b would be 12 (other > combinations are > >also possible), but how could I automatically find them? > perhaps a > >loop? > >? > >Apreciate the help, > >? > >Sincerely, > >? > >Andras > >??? [[alternative HTML version deleted]] > > > > > > > >------------------------------------------------------------------------ > > > >______________________________________________ > >R-help at r-project.org > mailing list > >https://stat.ethz.ch/mailman/listinfo/r-help > >PLEASE do read the posting guide > >http://www.R-project.org/posting-guide.html > >and provide commented, minimal, self-contained, > reproducible code. > >
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
The unlist-sapply-seq_len bit is unnecessarily convoluted, since the infcprodessa function can accept vector inputs. z <- infcprodessa( ab$a, TINF, ab$b, ab$b-TINF ) possibles <- ab[ z >= 15 & z <= 20, ] possibles[ which.min( possibles$a ), ] --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Berend Hasselman <bhh at xs4all.nl> wrote:> >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 > >______________________________________________ >R-help at r-project.org mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.