Jones, Glen R
2005-May-12 01:35 UTC
[R] Multiple IF statements - is there a better alternative?
Hello, Rather than rely on a set of IF statements (as there could be many - please see below)), could the following be done in a different/better way? InternalMean <- mean(data1[,3]) if (InternalMean == 0) Intresult = 1 if (InternalMean > 0 & InternalMean < 1) Intresult = .95 if (InternalMean >= 1 & InternalMean < 2) Intresult = .85 if (InternalMean >= 2 & InternalMean < 4) Intresult = .70 ... if (InternalMean >= 9) Intresult = .0 Thanks in advance Glen Jones Value Analyst Industry Framework Governance Telstra Corporation Limited> Tel: (03) 9634 7280email: glen.jones@team.telstra.com> The information contained in this e-mail message may be confidential. > If you are not the intended recipient, any use of, interference with, > disclosure or copying of this material is unauthorised and prohibited. > If you have received this message in error, please notify me by reply > e-mail and then delete the message. >[[alternative HTML version deleted]]
Patrick Connolly
2005-May-12 02:57 UTC
[R] Multiple IF statements - is there a better alternative?
On Thu, 12-May-2005 at 11:35AM +1000, Jones, Glen R wrote: |> Hello, |> |> Rather than rely on a set of IF statements (as there could be many - |> please see below)), could the following be done in a different/better |> way? |> |> InternalMean <- mean(data1[,3]) |> |> if (InternalMean == 0) |> Intresult = 1 |> if (InternalMean > 0 & InternalMean < 1) |> Intresult = .95 |> if (InternalMean >= 1 & InternalMean < 2) |> Intresult = .85 |> if (InternalMean >= 2 & InternalMean < 4) |> Intresult = .70 |> ... |> if (InternalMean >= 9) |> Intresult = .0 ?switch HTH -- Patrick Connolly HortResearch Mt Albert Auckland New Zealand Ph: +64-9 815 4200 x 7188 ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~ I have the world`s largest collection of seashells. I keep it on all the beaches of the world ... Perhaps you`ve seen it. ---Steven Wright ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~
Suresh Krishna
2005-May-12 03:42 UTC
[R] Multiple IF statements - is there a better alternative?
are you looking for something like: InternalMean <- mean(data1[,3]) TestValues <- c(0,1,2,4,9) #should be in increasing order TestResults <- c(.95, .85, .7, NaN,0) if (InternalMean==0) IntResult=1 else IntResult=TestResults[which(TestValues==max(TestValues[TestValues<InternalMean]))] -s. Jones, Glen R wrote:> Hello, > > Rather than rely on a set of IF statements (as there could be many - > please see below)), could the following be done in a different/better > way? > > InternalMean <- mean(data1[,3]) > > if (InternalMean == 0) > Intresult = 1 > if (InternalMean > 0 & InternalMean < 1) > Intresult = .95 > if (InternalMean >= 1 & InternalMean < 2) > Intresult = .85 > if (InternalMean >= 2 & InternalMean < 4) > Intresult = .70 > ... > if (InternalMean >= 9) > Intresult = .0 > > Thanks in advance > > > Glen Jones > Value Analyst > Industry Framework Governance > Telstra Corporation Limited > >>Tel: (03) 9634 7280 > > email: glen.jones at team.telstra.com > > >>The information contained in this e-mail message may be confidential. >>If you are not the intended recipient, any use of, interference with, >>disclosure or copying of this material is unauthorised and prohibited. >>If you have received this message in error, please notify me by reply >>e-mail and then delete the message. >> > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Suresh Krishna
2005-May-12 04:01 UTC
[R] Multiple IF statements - is there a better alternative?
oops, i meant something more like: TestValues <- c(0,1,2,4,9) #should be in increasing order TestResults <- c(.95, .85, .7, NaN,0) if (InternalMean==0) IntResult=1 else IntResult=TestResults[TestValues==max(TestValues[TestValues<=InternalMean])] -s. Suresh Krishna wrote:> > are you looking for something like: > > InternalMean <- mean(data1[,3]) > > TestValues <- c(0,1,2,4,9) #should be in increasing order > TestResults <- c(.95, .85, .7, NaN,0) > > if (InternalMean==0) IntResult=1 else > IntResult=TestResults[which(TestValues==max(TestValues[TestValues<InternalMean]))] > > > -s. > > Jones, Glen R wrote: > >> Hello, >> >> Rather than rely on a set of IF statements (as there could be many - >> please see below)), could the following be done in a different/better >> way? >> >> InternalMean <- mean(data1[,3]) >> >> if (InternalMean == 0) >> Intresult = 1 >> if (InternalMean > 0 & InternalMean < 1) >> Intresult = .95 >> if (InternalMean >= 1 & InternalMean < 2) >> Intresult = .85 >> if (InternalMean >= 2 & InternalMean < 4) >> Intresult = .70 >> ... >> if (InternalMean >= 9) >> Intresult = .0 >> >> Thanks in advance >> >> >> Glen Jones >> Value Analyst >> Industry Framework Governance >> Telstra Corporation Limited >> >>> Tel: (03) 9634 7280 >> >> >> email: glen.jones at team.telstra.com >> >> >>> The information contained in this e-mail message may be confidential. >>> If you are not the intended recipient, any use of, interference with, >>> disclosure or copying of this material is unauthorised and prohibited. >>> If you have received this message in error, please notify me by reply >>> e-mail and then delete the message. >>> >> >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at stat.math.ethz.ch mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide! >> http://www.R-project.org/posting-guide.html >> > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Brahm, David
2005-May-12 14:41 UTC
[R] Multiple IF statements - is there a better alternative?
Glen Jones <Glen.Jones at team.telstra.com> wrote:> if (InternalMean == 0) > Intresult = 1 > if (InternalMean > 0 & InternalMean < 1) > Intresult = .95 > [etc.]This looks like a job for "cut": R> i <- cut(InternalMean, c(-Inf,0,1,2,4,9,Inf), labels=F) R> Intresult <- c(1,.95,.85,.70,.50,0)[i] -- David Brahm (brahm at alum.mit.edu)