Re: [R] Counting confidence intervals
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.
Re: [R] Counting confidence intervals
You should look at findInterval. Used with as.numeric it could do what you request although it has a much wider range of uses. -- David Sent from my iPhone On Mar 20, 2013, at 5:15 PM, Greg Snow 538...@gmail.com wrote: 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. __ 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.
Re: [R] Counting confidence intervals
There _is_ a function ?within. Drat! of course there is. I even use it, though not often. Maybe your function can be named 'between' Good thought - thanks Steve E *** This email and any attachments are confidential. Any use...{{dropped:8}} __ 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.
Re: [R] Counting confidence intervals
Hi, I have a 2 x 1 matrix of confidence intervals. The first column is the lower and the next column is the upper. I want to cont how many times a number say 12 lies in the interval. Can anyone assist? -- Thanks, Jim. [[alternative HTML version deleted]] __ 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.
Re: [R] Counting confidence intervals
Hi Jim, Try either of the following (untested): sum( x[1, ] 12 x[2, ] 12) sum(apply(x, 2, function(x) x[1] 12 x[2] 12)) where x is your 2x1000 matrix. HTH, Jorge.- On Tue, Mar 19, 2013 at 12:03 AM, Jim Silverton wrote: Hi, I have a 2 x 1 matrix of confidence intervals. The first column is the lower and the next column is the upper. I want to cont how many times a number say 12 lies in the interval. Can anyone assist? -- Thanks, Jim. [[alternative HTML version deleted]] __ 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. [[alternative HTML version deleted]] __ 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.
Re: [R] Counting confidence intervals
sum(M[1]12 12=M[2]) untested, no data --- Jeff NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. Jim Silverton jim.silver...@gmail.com wrote: Hi, I have a 2 x 1 matrix of confidence intervals. The first column is the lower and the next column is the upper. I want to cont how many times a number say 12 lies in the interval. Can anyone assist? __ 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.
Re: [R] Counting confidence intervals
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. From: Jim Silverton jim.silver...@gmail.com To: r-help@r-project.org Sent: Monday, March 18, 2013 9:03 AM Subject: Re: [R] Counting confidence intervals Hi, I have a 2 x 1 matrix of confidence intervals. The first column is the lower and the next column is the upper. I want to cont how many times a number say 12 lies in the interval. Can anyone assist? -- Thanks, Jim. [[alternative HTML version deleted]] __ 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. __ 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.
Re: [R] Counting confidence intervals
Thanks. Jeff On Mon, Mar 18, 2013 at 9:30 AM, Jeff Newmiller jdnew...@dcn.davis.ca.uswrote: sum(M[1]12 12=M[2]) untested, no data --- Jeff NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. Jim Silverton jim.silver...@gmail.com wrote: Hi, I have a 2 x 1 matrix of confidence intervals. The first column is the lower and the next column is the upper. I want to cont how many times a number say 12 lies in the interval. Can anyone assist? -- Thanks, Jim. [[alternative HTML version deleted]] __ 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.
Re: [R] Counting confidence intervals
Thats cumbersome, Arun. sum(mat1[,1] 12 mat1[,2] 12) [1] 17 will do the job and even faster: system.time(replicate(1, sum(mat1[,1] 12 mat1[,2] 12))) # user system elapsed # 0.067 0.001 0.078 HTH, Jorge.- On Tue, Mar 19, 2013 at 1:06 AM, arun 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. From: Jim Silverton To: r-help@r-project.org Sent: Monday, March 18, 2013 9:03 AM Subject: Re: [R] Counting confidence intervals Hi, I have a 2 x 1 matrix of confidence intervals. The first column is the lower and the next column is the upper. I want to cont how many times a number say 12 lies in the interval. Can anyone assist? -- Thanks, Jim. [[alternative HTML version deleted]] __ 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. __ 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. [[alternative HTML version deleted]] __ 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.
Re: [R] Counting confidence intervals
Hi, Jorge's method will be faster. #system.time(res1-sum(apply(mat2,1,function(x) x[1]12 x[2]12))) #instead of 2, it should be 1 # user system elapsed # 0.440 0.000 0.445 system.time(res1-sum(apply(mat2,1,function(x) x[1]=12 x[2]12))) # # user system elapsed # 0.500 0.000 0.502 res1 #[1] 80070 A.K. From: Jim Silverton jim.silver...@gmail.com To: arun smartpink...@yahoo.com Sent: Monday, March 18, 2013 10:08 AM Subject: Re: [R] Counting confidence intervals thanks arun!! On Mon, Mar 18, 2013 at 10:06 AM, arun smartpink...@yahoo.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. From: Jim Silverton jim.silver...@gmail.com To: r-help@r-project.org Sent: Monday, March 18, 2013 9:03 AM Subject: Re: [R] Counting confidence intervals Hi, I have a 2 x 1 matrix of confidence intervals. The first column is the lower and the next column is the upper. I want to cont how many times a number say 12 lies in the interval. Can anyone assist? -- Thanks, Jim. [[alternative HTML version deleted]] __ 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. -- Thanks, Jim. __ 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.
Re: [R] Counting confidence intervals
If you don't use apply() it would be even faster: system.time(sum(mat2[,1] 12 mat2[,2] 12)) user system elapsed 0.004 0.000 0.003 Regards, Jorge.- On Tue, Mar 19, 2013 at 1:21 AM, arun wrote: Hi, Jorge's method will be faster. #system.time(res1-sum(apply(mat2,1,function(x) x[1]12 x[2]12))) #instead of 2, it should be 1 # user system elapsed # 0.440 0.000 0.445 system.time(res1-sum(apply(mat2,1,function(x) x[1]=12 x[2]12))) # # user system elapsed # 0.500 0.000 0.502 res1 #[1] 80070 A.K. From: Jim Silverton jim.silver...@gmail.com To: arun smartpink...@yahoo.com Sent: Monday, March 18, 2013 10:08 AM Subject: Re: [R] Counting confidence intervals thanks arun!! On Mon, Mar 18, 2013 at 10:06 AM, arun smartpink...@yahoo.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. From: Jim Silverton jim.silver...@gmail.com To: r-help@r-project.org Sent: Monday, March 18, 2013 9:03 AM Subject: Re: [R] Counting confidence intervals Hi, I have a 2 x 1 matrix of confidence intervals. The first column is the lower and the next column is the upper. I want to cont how many times a number say 12 lies in the interval. Can anyone assist? -- Thanks, Jim. [[alternative HTML version deleted]] __ 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. -- Thanks, Jim. [[alternative HTML version deleted]] __ 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.
Re: [R] Counting confidence intervals
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}} __ 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.
Re: [R] Counting confidence intervals
Hello, There _is_ a function ?within. Maybe your function can be named 'between' Rui Barradas Em 18-03-2013 16:16, S Ellison escreveu: 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}} __ 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. __ 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.