[R] arithmetic comparison over corresponding values from two vectors
marc_schwartz at comcast.net
Tue Feb 10 18:19:50 CET 2009
on 02/10/2009 11:02 AM T Joshi wrote:
> I have scenario in which I wish to check whether numeric values in one array
> falls within the range of numbers defined over corresponding values in two
> other vectors:
> starts = c(12,45,67,110)
> ends=c(24, 58,102,150)
> The result should be "1,3" , indices of vector starts/ends.
> which(trgroup>=starts & trgroup<=ends) wouldn't give required result.
> I don't want to write a for loop for doing this. Kindly help with ideas.
> sapply(trgroup, function(x) which((x >= starts) & (x <= ends)))
 1 3
More information about the R-help