I'm trying to get a handle on the ubiquitous which
function. Until I started reading questions/answers on SO I never found the need for it. And I still don't.
As I understand it, which
takes a Boolean vector and returns a weakly shorter vector containing the indices of the elements which were true:
> seq(10) [1] 1 2 3 4 5 6 7 8 9 10 > x <- seq(10) > tf <- (x == 6 | x == 8) > tf [1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE FALSE FALSE > w <- which(tf) > w [1] 6 8
So why would I ever use which
instead of just using the Boolean vector directly? I could maybe see some memory issues with huge vectors, since length(w)
<< length(tf)
, but that's hardly compelling. And there are some options in the help file which don't add much to my understanding of possible uses of this function. The examples in the help file aren't of much help either.
Edit for clarity-- I understand that the which
returns the indices. My question is about two things: 1) why you would ever need to use the indices instead of just using the boolean selector vector? and 2) what interesting behaviors of which
might make it preferred to just using a vectorized Boolean comparison?
[M] [T] I didn't know for certain which train to take. [M] [T] She told me which clothes would be good to wear. [M] [T] Which do you like better, white wine or red wine? [M] [T] Which air conditioner do you think is the most efficient?
Luckily there's an easy way to remember whether to use that or which. If the relative clause contains information that is not essential to the meaning of the sentence, and is also preceded by a comma, a dash, or parenthesis, it's probably nonrestrictive, so use which. If not, odds are it's restrictive, so use that.
Okay, here is something where it proved useful last night:
In a given vector of values what is the index of the 3rd non-NA value?
> x <- c(1,NA,2,NA,3) > which(!is.na(x))[3] [1] 5
A little different from DWin's use, although I'd say his is compelling too!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With