ihok at hotmail.com
Fri Nov 25 20:23:13 CET 2011
I have a named vector:
> z <- c(1, 2, 3, 2)
> names(z) <- c("a","b","c","b")
> f <- c("b","c")
I want to know the index in z of the first occurrence of each of the values in f.
One implementation is
> sapply(f, function(x) which(names(z)==x))
Is which() smart enough to stop when it finds in z the first occurrence of every
value from f, or does it search through all the values in z only to report the
Are some more elegant ways of writing this code?
More information about the R-help