Hi, I have a 2 x 10000 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]]
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 10000 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]]
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, >I have a 2 x 10000 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?
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.silverton at gmail.com> To: r-help at r-project.org Sent: Monday, March 18, 2013 9:03 AM Subject: Re: [R] Counting confidence intervals Hi, I have a 2 x 10000 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 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.
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.silverton at gmail.com> To: arun <smartpink111 at 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 <smartpink111 at 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.silverton at gmail.com> >To: r-help at r-project.org >Sent: Monday, March 18, 2013 9:03 AM >Subject: Re: [R] Counting confidence intervals > > >Hi, >I have a 2 x 10000 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 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. >-- Thanks, Jim.