[R] Determine breaks based on a break type...

Jim Lemon jim at bitwrit.com.au
Sat Mar 22 23:38:44 CET 2014


On 03/23/2014 03:15 AM, Jonathan Greenberg wrote:
> R-helpers:
>
> I was wondering, given a vector of data, if there is a way to
> calculate the break points based on the breaks= parameter from
> histogram, but skipping all the other calculations (all I want is the
> breakpoints, not the frequencies).  I can, of course, simply run the
> histogram and extract the break component:
>
> mybreaks<- hist(runif(100))$breaks
>
> But is there a faster way to do this, if this is all I want?
>
Hi Jonathan,
I assume that you want a function to define the number of classes rather 
than the alternative methods of simply specifying the number of breaks 
or the actual locations of breaks yourself. I know of three such functions:

nclass.Sturges
nclass.scott
nclass.FD

all in the grDevices package. You can call these functions directly and 
then calculate the break point locations by dividing the range of the 
observations into calculated number of classes:

x<-1:24
ncls<-nclasses.Sturges(x)
ncls
[1] 6
# now decide whether you want right-closed intervals (the default)
# and pad the left (default) or right side
intwidth<-(max(x) - (min(x)-1))/6
breaks<-seq(min(x)-1,max(x),by=intwidth)

It is debatable whether this is better than:

hist(x,plot=FALSE)$breaks

Jim




More information about the R-help mailing list