[R] table questions

Jim Lemon jim at bitwrit.com.au
Wed Jul 30 12:31:22 CEST 2008


On Tue, 2008-07-29 at 11:28 -0500, Edna Bell wrote:
> Hi again!
> 
> 
> Suppose I have the following:
> 
> > xy <- round(rexp(20),1)
> > xy
>  [1] 0.1 3.4 1.6 0.4 1.0 1.4 0.2 0.3 1.6 0.2 0.0 0.1 0.1 1.0 2.0 0.9
> 2.5 0.1 1.5 0.4
> > table(xy)
> xy
>   0 0.1 0.2 0.3 0.4 0.9   1 1.4 1.5 1.6   2 2.5 3.4
>   1   4   2   1   2   1   2   1   1   2   1   1   1
> >
> Is there a way to set things up to have
> 0 - 0.4   0.5 - 0.9  etc. please?
> 
> I know there is the cut functions, but breaks are required.  If you
> don't have breaks, what should you do, please?
> 
> Would using the breaks from the hist function work appropriately, please?
> 
Well, Edna, this looked like fun, so here is something that may help you
out.

lumpyTable<-function(x,breaks) {
 xcounts<-hist(x,breaks=breaks,plot=FALSE)$counts
 nbreaks<-length(breaks)
 binnames<-paste(breaks[1:(nbreaks-1)],
  breaks[2:nbreaks],sep="-",collapse="\t")
 cat(binnames,"\n",paste(xcounts,collapse="\t"),"\n")
}
xy<-round(rexp(20),1)
lumpyTable(xy,c(0,0.5,1,2,4))

Jim



More information about the R-help mailing list