Skip to content
Prev 173885 / 398503 Next

Testing for Inequality à la "select case"

On Sun, Mar 15, 2009 at 11:46 PM, diegol <diegol81 at gmail.com> wrote:
Actually Stavros (???????), not Stephen/Steve (????????).  Both Greek,
but different names.
Approach is vectorized over the range parameter, but not vectorized
over the x parameter.  If you want to vectorize over x, you can use
findInterval:

mr <-
 local({
   # Local constants
   range= c(0,20,100,250,700,1000,Inf)*1000
   perc = c(65,40,30,25,20,0)/100
   min =  c(0,14,40,75,175,250)*1000

   function(x)
     { idx <- findInterval(x,range)
       pmax( x*perc[idx], min[idx] )
     }
 })

And this time, you *do* need pmax.  I did refer to cut/split, but only
to say they were unnecessary.

          -s