[R] Proportions again

Peter Dalgaard p.dalgaard at biostat.ku.dk
Sun Feb 29 16:14:13 CET 2004


Andrew Robinson <andrewr at uidaho.edu> writes:

> table(sex)/length(sex)

or, as is the intended usage:

> prop.table(table(sex))
sex
    1     2
0.375 0.625


> > I asked before and it was great, cause as a beginner I learned a lot. But, 
> if I have this in R (1 and 2 are codes for sex):
> > > sex<-c(1,2,2,1,1,2,2,2)
> > > sex
> >
> > [1] 1 2 2 1 1 2 2 2
> >
> > I´d like to obtain the proportion according to sex.So I type:
> > > prop.table(sex)
> >
> > [1] 0.07692308 0.15384615 0.15384615 0.07692308 0.07692308 0.15384615
> > 0.15384615 [8] 0.15384615
> >
> > The result is OK, but I expected to see a simple frequency table or
> > something like that:
> >
> > 1   0.375
> > 2   0.625
> >      1.0
> >
> > How can I get this ?


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list