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 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.
> >
>
>
> --
> 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.

Reply via email to