What’s the use of which?

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!

Leave a Comment