[R] Testing for Inequality à la "select case"

baptiste auguie ba208 at exeter.ac.uk
Sun Mar 15 21:27:57 CET 2009


Hi,

I think you could get a cleaner solution using ?cut to split your data  
in given ranges (the break argument), and then using this factor to  
give the appropriate percentage.


Hope this helps,

baptiste

On 15 Mar 2009, at 20:12, diegol wrote:

>
> Using R 2.7.0 under WinXP.
>
> I need to write a function that takes a non-negative vector and  
> returns the
> parallell maximum between a percentage of this argument and a fixed  
> value.
> Both the percentages and the fixed values depend on which interval x  
> falls
> in. Intervals are as follows:
>
>> From      |       To         |       % of x   |       Minimum
> ---------------------------------------------------------------
> 0           |       20000    |       65        |       0
> 20000     |       100000  |       40        |       14000	
> 100000   |       250000   |       30       |       40000	
> 250000   |       700000   |       25       |       75000
> 700000   |       1000000 |       20       |       175000
> 1000000 |       inf          |       --       |       250000
>
> Once the interval is determined, the values in x are multiplied by the
> percentages applying to the range in the 3rd column.
> If the result is less than the fourth column, then the latter is used.
> For values of x falling in the last interval, 250,000 must be used.
>
>
> My best attempt at it in R:
>
> 	MyRange <- function(x){
>
> 	range_aux = ifelse(x<=20000, 1,
>        	    ifelse(x<=100000, 2,
> 	              ifelse(x<=250000, 3,
>        	        ifelse(x<=700000, 4,
>                	  ifelse(x<=1000000, 5,6)))))
> 	percent = c(0.65, 0.4, 0.3, 0.25, 0.2, 0)
> 	minimum = c(0, 14000, 40000, 75000, 175000, 250000)
>
> 	pmax(x * percent[range_aux], minimum[range_aux])
>
> 	}
>
>
> This could be done in Excel much tidier in my opinion (especially the
> range_aux part), element by element (cell by cell),
>
> with a VBA function as follows:
>
> 	Function MyRange(x as Double) as Double
>
> 	Select Case x
> 	    Case Is <= 20000
>        	MyRange = 0.65 * x
> 	    Case Is <= 100000
> 	        RCJuiProfDet = IIf(0.40 * x < 14000, 14000, 0.4 * x)
> 	    Case Is <= 250000
> 	        RCJuiProfDet = IIf(0.3 * x < 40000, 40000, 0.3 * x)
> 	    Case Is <= 700000
> 	        RCJuiProfDet = IIf(0.25 * x < 75000, 75000, 0.25 * x)
> 	    Case Is <= 1000000
> 	        RCJuiProfDet = IIf(0.2 * x < 175000, 175000, 0.2 * x)
> 	    Case Else
> 		' This is always 250000. I left it this way so it is analogous to  
> the R
> function
> 	        RCJuiProfDet = IIf(0 * x < 250000, 250000, 0 * x)
> 	End Select
>
> 	End Function
>
>
> Any way to improve my R function? I have searched the help archive  
> and the
> closest I have found is the switch function, which tests for  
> equality only.
> Thank you in advance for reading this.
>
>
> -----
> ~~~~~~~~~~~~~~~~~~~~~~~~~~
> 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-tp22527465p22527465.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag




More information about the R-help mailing list