Skip to content
Prev 173829 / 398503 Next

Testing for Inequality à la "select case"

Steve, I still don't understand the analogy. I agree that in this case the R
approach is vectorized. However, your function just as you first proposed it
will not work without a loop.
It's true. I changed it because I had applied your original version of mr()
to the entire vector x, which gave an incorrect result (perhaps "range" was
recycled in "idx <- which(x<=range)[1]"). If I used max instead of pmax, and
ever happened to use mr() without a loop, the length of the result would be
strange enough for me to realise the error. But then again, I added the "if
(length(x) >1) stop("x must have length 1")" line, so using max or pmax now
doesn't really make a difference, apart perhaps from run time.
I could edit the mr_2() function a little bit to make it more efficient. I
left it mostly unchanged for the thread to be easier to follow. For example
I could replace the last four lines for only:

    product <- x*percent
    ifelse(product< minimum, minimum, product)

But I believe you refer to the cut/split functions rather. I agree that
"which(x<=range)[1]" is straighforward, but using such expression will
require a loop to pull the trick, which I don't intend. Am I missing
something?


Regards,
Diego
Stavros Macrakis-2 wrote:
-----
~~~~~~~~~~~~~~~~~~~~~~~~~~
Diego Mazzeo
Actuarial Science Student
Facultad de Ciencias Econ?micas
Universidad de Buenos Aires
Buenos Aires, Argentina