[R] Count the number of occurences in ranges

torpedo fisken torpedofisken at gmail.com
Thu Jul 16 05:26:41 CEST 2009


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




More information about the R-help mailing list