Re: [R] Testing for Inequality à la select case

2009-03-16 Thread Stavros Macrakis
On Sun, Mar 15, 2009 at 11:46 PM, diegol diego...@gmail.com wrote: ...Steve, ... Actually Stavros (ΣΤΑΥΡΟΣ), not Stephen/Steve (ΣΤΕΦΑΝΟΣ). Both Greek, but different names. I still don't understand the analogy. I agree that in this case the R approach is vectorized. However, your function

[R] Testing for Inequality à la select case

2009-03-15 Thread diegol
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 |

Re: [R] Testing for Inequality à la select case

2009-03-15 Thread baptiste auguie
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

Re: [R] Testing for Inequality à la select case

2009-03-15 Thread Stavros Macrakis
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

Re: [R] Testing for Inequality à la select case

2009-03-15 Thread diegol
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

Re: [R] Testing for Inequality à la select case

2009-03-15 Thread diegol
Hello Baptiste, I am not very sure how I'd go about that. Taking the range, perc and min vectors from Stavros' response: 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 For range to work as the breaks argument to cut, I think

Re: [R] Testing for Inequality à la select case

2009-03-15 Thread baptiste auguie
Hi, I don't use ?cut and ?split very much either, so this may not be good advice. From what I understood of your problem, I would try something along those lines, 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 range = c(0, range)

Re: [R] Testing for Inequality à la select case

2009-03-15 Thread diegol
Hello Baptiste, Thanks so much for your help. This function which is basically your input wrapped with curly brackets seems to work alright: mr_2 - function(x){ 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

Re: [R] Testing for Inequality à la select case

2009-03-15 Thread Stavros Macrakis
Using cut/split seems like gross overkill here. Among other things, you don't need to generate labels for all the different ranges. which(x=range)[1] seems straightforward enough to me, but you could also use the built-in function findInterval. -s

Re: [R] Testing for Inequality à la select case

2009-03-15 Thread diegol
That's what I meant by element-by -element. A vector in R corresponds to a row or a column in Excel, and a vector operation in R corresponds to a row or column of formulae, e.g. Excel A B C 1) 5 10 a1+b1 (= 15) 2) 3 2 a2+b2 (= 5) etc. R A -