Skip to content

Counting confidence intervals

11 messages · Jorge I Velez, Jeff Newmiller, Jim Silverton +5 more

#
sum(M[1]<12 & 12<=M[2]) untested, no data
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
Jim Silverton <jim.silverton at gmail.com> wrote:

            
#
Hi,
Try this:
set.seed(25)
mat1<- matrix(cbind(sample(1:15,20,replace=TRUE),sample(16:30,20,replace=TRUE)),ncol=2)
?nrow(mat1[sapply(seq_len(nrow(mat1)),function(i) any(seq(mat1[i,1],mat1[i,2])==12)),])
#[1] 17


set.seed(25)
mat2<- matrix(cbind(sample(1:15,1e5,replace=TRUE),sample(16:30,1e5,replace=TRUE)),ncol=2)

system.time(res<-nrow(mat2[sapply(seq_len(nrow(mat2)),function(i) any(seq(mat2[i,1],mat2[i,2])==12)),]))
?#? user? system elapsed 
?# 1.552?? 0.000?? 1.549 
res
#[1] 80070
?head(mat2[sapply(seq_len(nrow(mat2)),function(i) any(seq(mat2[i,1],mat2[i,2])==12)),])
#???? [,1] [,2]
#[1,]??? 7?? 29
#[2,]?? 11?? 30
#[3,]??? 3?? 30
#[4,]??? 2?? 26
#[5,]?? 10?? 22
#[6,]??? 6?? 22
A.K.
#
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}}
#
Hello,

There _is_ a function ?within. Maybe your function can be named 'between'

Rui Barradas

Em 18-03-2013 16:16, S Ellison escreveu:
#
Drat! of course there is. I even use it, though not often.
Good thought - thanks

Steve E

*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}
1 day later
#
You should look at findInterval. Used with as.numeric it could do what you request although it has a much wider range of uses.