[R] table questions
(Ted Harding)
Ted.Harding at manchester.ac.uk
Wed Jul 30 02:24:24 CEST 2008
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 Bell
It 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 ------------------------------
More information about the R-help
mailing list