Testing for Inequality à la "select case"
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 <- c(5,3)
B <- c(10,2)
C <- A + B
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.
max and pmax are equivalent in this case. I just use pmax as my default because it acts like other arithmetic operators (+, *, etc.) which perform pointwise (element-by-element) operations.
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.
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,
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:
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
______________________________________________ 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.
----- ~~~~~~~~~~~~~~~~~~~~~~~~~~ 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-tp22527465p22531513.html Sent from the R help mailing list archive at Nabble.com.