The TeachingDemos package has %<% and %<=% functions that can be chained simply, so you could do something like:
sum( 5:1 %<=% 1:5 %<=% 10:14 ) and other similar approaches. The idea is that you can do comparisons as: lower %<% x %<% upper instead of lower < x & x < upper On Mon, Mar 18, 2013 at 10:16 AM, S Ellison <s.elli...@lgcgroup.com> wrote: > > > I want to cont how many > > > times a number say 12 lies in the interval. Can anyone assist? > > Has anyone else ever wished there was a moderately general 'inside' or > 'within' function in R for this problem? > > For example, something that behaves more or less like > > within <- function(x, interval=NULL, closed=c(TRUE, TRUE), > lower=min(interval), upper=max(interval)) { > #interval must be a length 2 vector > #closed is taken in the order (lower, upper) > #lower and upper may be vectors and will be recycled (by "<" etc) > if not of length length(x) > > low.comp <- if(closed[1]) "<=" else "<" > high.comp <- if(closed[2]) ">=" else ">" > > do.call(low.comp, list(lower, x)) & do.call(high.comp, list(upper, > x)) > } > > > #Examples > within(1:5, c(2,4)) > > within(1:5, c(2,4), closed=c(FALSE, TRUE)) > > within(1:5, lower=5:1, upper=10:14) > > > S Ellison > LGC > > ******************************************************************* > This email and any attachments are confidential. Any u...{{dropped:19}} ______________________________________________ R-help@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.