An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090210/1b8ea40b/attachment-0001.pl>
arithmetic comparison over corresponding values from two vectors
4 messages · T Joshi, Marc Schwartz, Keith Jewell +1 more
on 02/10/2009 11:02 AM T Joshi wrote:
Hi, 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) trgroup=c(18,87) 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. Josh
Try this:
sapply(trgroup, function(x) which((x >= starts) & (x <= ends)))
[1] 1 3 HTH, Marc Schwartz
sapply(trgroup, function(x) which(x>=starts & x <=ends)) works for your example data, but there's probably a better way HTH KJ "T Joshi" <tejalonline at gmail.com> wrote in message news:11417a880902100902w53664a3dq11aee64a963d8383 at mail.gmail.com...
Hi, 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) trgroup=c(18,87) 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. Josh
On Tue, 10 Feb 2009, Keith Jewell wrote:
sapply(trgroup, function(x) which(x>=starts & x <=ends)) works for your example data, but there's probably a better way
For large scale problems this works well: http://finzi.psych.upenn.edu/R/Rhelp02a/archive/60815.html HTH, Chuck
HTH KJ "T Joshi" <tejalonline at gmail.com> wrote in message news:11417a880902100902w53664a3dq11aee64a963d8383 at mail.gmail.com...
Hi, 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) trgroup=c(18,87) 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. Josh
______________________________________________ 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.
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901