Hello Stavros, > If you'd do it element-by-element in Excel, why not do it > element-by-element in R?
Well, actually I was hoping for a vectorized solution so as to avoid looping. I need to use this formula on rather lengthy vectors and I wanted to put R's efficiency to some good use. In any case, I had not come up with your solution. For now, I'd stick to my ugly version. > Make sense? Perfectly. >x <- 1:150 * 10000 >y <- numeric(150) >for (i in 1:150) y[i] <- mr(x[i]) >identical(MyRange(x), y) >TRUE I would however use max instead of pmax, since the argument for mr() must be a vector of length 1. The final version looks like this (also added a line to avoid vectors of length > 1): mr <- local({ # Local constants range= c(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) {if (length(x) >1) stop("x must have length 1") idx <- which(x<=range)[1] max( x*perc[idx], min[idx] ) } }) Thank you very much for your help. Diego Stavros Macrakis-2 wrote: > > On Sun, Mar 15, 2009 at 4:12 PM, diegol <diego...@gmail.com> wrote: >> ...This could be done in Excel much tidier in my opinion (especially the >> range_aux part), element by element (cell by cell)... > > If you'd do it element-by-element in Excel, why not do it > element-by-element in R? > > Create a table with the limits of the ranges > > range= c(20,100,250,700,1000,Inf)*1000 > > and then find the index of the appropriate case using something like > > idx <- which(x<=range)[1] > > Then the formula becomes simply > > pmax( x*perc[idx], min[idx] ) > > Putting it all together: > > mr <- > local({ > # Local constants > range= c(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 <- which(x<=range)[1] > pmax( x*perc[idx], min[idx] ) > } > }) > > Make sense? > > -s > > ______________________________________________ > R-help@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. > > ----- ~~~~~~~~~~~~~~~~~~~~~~~~~~ Diego Mazzeo Actuarial Science Student Facultad de Ciencias Económicas Universidad de Buenos Aires Buenos Aires, Argentina -- View this message in context: http://www.nabble.com/Testing-for-Inequality-%C3%A0-la-%22select-case%22-tp22527465p22529091.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@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.