[R] Count the number of occurences in ranges

torpedo fisken torpedofisken at gmail.com
Fri Jul 17 06:04:15 CEST 2009


Thanks, 'cut()' was indeed what I was looking for

thanks

2009/7/16  <markleeds at verizon.net>:
> Hi: I'm not sure if I understand what you want but below might help you ?
> see cut for more details by doing ?cut.
>
>
> probs<-c(0.001,0.5,0.02,.05,0.12,0.23,0.5,0.6,0.59,0.7)
> probs
>
> temp <- cut(probs,breaks=seq(0,1,length=51))
> print(temp)
>
> do.call(rbind,lapply(split(probs,temp),sum))
>
>
>
> On Jul 15, 2009, torpedo fisken <torpedofisken at gmail.com> wrote:
>
> I got a vector of probabilities like,
> probs<-c(0.001,0.5,0.02,1,.....)
>
> Is there any nice and easy builtin function to get the number of
> occurences within some specified probabality range.
> Like with 2% it would be
>
> occur[1] = sum(probs[probs>0&probs<0.02])
> occur[2] = sum(probs[probs>0.02&probs<0.04])
> ...
> occur[50] =sum(probs[probs>0.09] & probs<1)
>
> (If it was a discrete space I would use 'table()' directly)
>
> I made a function that looks something like
> sorted <-sort(probs)
> splits <- seq(0,1,0.02)
>
> occur <- vector(mode="numeric",len=length(splits))
> occur[1] <- sum(sorted<splits[2])
>
> for(i in 2:(length(splits)-1))
> occur[i] <- sum(sorted<splits[i+1]) - sum(occur[1:i])
>
> This seems to do what I want, but I guess there must be a more
> beautifull way of doing it.
>
> Thanks in advance
>
> ______________________________________________
> 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.
>




More information about the R-help mailing list