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? thanks Edna Bell
# is that what you want? table(cut(xy,seq(0,max(xy)+.4,by=.4))) # or this table(cut(xy,hist(xy)$breaks)) # not the same regards, PF +------------------------------------------------- | Patrizio Frederic | Research associate in Statistics, | Department of Economics, | University of Modena and Reggio Emilia, | Via Berengario 51, | 41100 Modena, Italy | | tel: +39 059 205 6727 | fax: +39 059 205 6947 | mail: patrizio.frederic at unimore.it +------------------------------------------------- 2008/7/29 Edna Bell <edna.bell01 at gmail.com>:> 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? > > thanks > Edna Bell > > ______________________________________________ > 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. >
Sorry, in my copy&paste I left out an essential line in the code below. Now present. Ted. On 29-Jul-08 16:28:26, 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? > > thanks > Edna BellIt could, though it seems a bit clumsy: xy<-round(rexp(20),1) H<-hist(xy,breaks=(-0.05+0.5*(0:8)),plot=FALSE) brks<-H$breaks ; cts<-H$counts ##Following line now inserted: k<-length(brks) Tbl<-rbind(NULL,cts) brlabs<-paste(brks[1:(k-1)]+0.05,"-",brks[2:k]-0.05,sep="") colnames(Tbl)<-brlabs Tbl # 0-0.4 0.5-0.9 1-1.4 1.5-1.9 2-2.4 2.5-2.9 3-3.4 3.5-3.9 #cts 5 4 6 3 0 1 1 0 One could fiddle with formatting to replace 0 by 0.0 and 3 by 3.0, and set rownames=NULL; but I don't see how to get the counts placed in the "middle" of the range. But it's better than nothing, I suppose! Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 30-Jul-08 Time: 01:14:43 ------------------------------ XFMail ------------------------------ -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 30-Jul-08 Time: 01:24:21 ------------------------------ XFMail ------------------------------
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