[R] Counting confidence intervals

S Ellison S.Ellison at LGCGroup.com
Mon Mar 18 17:16:03 CET 2013


> > 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 use...{{dropped:8}}



More information about the R-help mailing list