Hello. 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 ? Thank you very much [[alternative HTML version deleted]]
table(sex)/length(sex) Andrew On Sunday 29 February 2004 06:35, Carlos Mauricio Cardeal Mendes wrote:> Hello. > > 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 ? > > Thank you very much > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html-- Andrew Robinson Ph: 208 885 7115 Department of Forest Resources Fa: 208 885 6226 University of Idaho E : andrewr at uidaho.edu PO Box 441133 W : http://www.uidaho.edu/~andrewr Moscow ID 83843 Or: http://www.biometrics.uidaho.edu No statement above necessarily represents my employer's opinion.
> Carlos Mauricio Cardeal Mendes wrote: > 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 ?prop.table(table(sex)) -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 452-1424 (M, W, F) fax: (917) 438-0894
Dear Carlos, prop.table() takes a table as its argument, so you could specify prop.table(table(sex)). See ?prop.table for more details. John> -----Original Message----- > From: r-help-bounces+jfox=mcmaster.ca at stat.math.ethz.ch > [mailto:r-help-bounces+jfox=mcmaster.ca at stat.math.ethz.ch] On > Behalf Of Carlos Mauricio Cardeal Mendes > Sent: Sunday, February 29, 2004 9:35 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Proportions again > > Hello. > > 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 ? >
Several people have alrady answered you by this time and in addition to their answers you might also be interested in CrossTable in package gregmisc. --- Date: Sun, 29 Feb 2004 11:35:06 -0300 From: Carlos Mauricio Cardeal Mendes <mcardeal at atarde.com.br> To: <r-help at stat.math.ethz.ch> Subject: [R] Proportions again Hello. 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 ? Thank you very much
That's true; however, CrossTable(x,x) does give the desired counts and proportions in the margin line at the bottom. See the row labelled Column Total in the following example based on Carlos' vector:> sex<-c(1,2,2,1,1,2,2,2) > CrossTable(sex,sex)Cell Contents |-----------------| | N | | N / Row Total | | N / Col Total | | N / Table Total | |-----------------| Total Observations in Table: 8 | sex sex | 1 | 2 | Row Total | -------------|-----------|-----------|-----------| 1 | 3 | 0 | 3 | | 1.000 | 0.000 | 0.375 | | 1.000 | 0.000 | | | 0.375 | 0.000 | | -------------|-----------|-----------|-----------| 2 | 0 | 5 | 5 | | 0.000 | 1.000 | 0.625 | | 0.000 | 1.000 | | | 0.000 | 0.625 | | -------------|-----------|-----------|-----------| Column Total | 3 | 5 | 8 | | 0.375 | 0.625 | | -------------|-----------|-----------|-----------| --- Date: Sun, 29 Feb 2004 13:48:27 -0600 From: Marc Schwartz <MSchwartz at MedAnalytics.com> To: <ggrothendieck at myway.com> Cc: <mcardeal at atarde.com.br>,R-Help <r-help at stat.math.ethz.ch> Subject: RE: [R] Proportions again On Sun, 2004-02-29 at 12:40, Gabor Grothendieck wrote:> Several people have alrady answered you by this time and > in addition to their answers you might also be interested > in CrossTable in package gregmisc.Gabor, Thanks for pointing out CrossTable(). Just as a quick heads up/clarification for Carlos, CrossTable() is designed to cross-tabulate two vectors and generate counts, row/column/table proportions and other results from the 2 dimensional cross-tab in a (hopefully) nicely formatted fashion. It is not presently designed to handle generating proportions from the tabulation of a single vector with repeating values (such as Carlos' example) and will generate an error message if a single vector is passed. In that scenario, as many folks have already recommended, the combination of table() and prop.table() would be preferred. HTH, Marc Schwartz
I agree its overkill but prop.table generalizes to mulitple dimensions, in which case it becomes comparable to CrossTable, so I thought it was worth mentioning. By the way, as the author of CrossTable, perhaps you might might consider generalizing CrossTable to the 1D case too? --- Date: Sun, 29 Feb 2004 19:19:29 -0600 From: Marc Schwartz <MSchwartz at MedAnalytics.com> To: <ggrothendieck at myway.com> Cc: <mcardeal at atarde.com.br>,R-Help <r-help at stat.math.ethz.ch> Subject: RE: [R] Proportions again On Sun, 2004-02-29 at 18:27, Gabor Grothendieck wrote:> That's true; however, > > CrossTable(x,x) > > does give the desired counts and proportions in the margin > line at the bottom. See the row labelled Column Total in > the following example based on Carlos' vector: > > > sex<-c(1,2,2,1,1,2,2,2) > > CrossTable(sex,sex)snip OK....true. I had not considered that approach from a design perspective, but it does provide the requisite information. One could feasibly reduce some of the complexity of the table by setting some of the prop.* arguments to false: CrossTable(sex, sex, prop.c = FALSE, prop.t = FALSE) which would yield: Cell Contents |-----------------| | N | | N / Row Total | |-----------------| Total Observations in Table: 8 | sex sex | 1 | 2 | Row Total | -------------|-----------|-----------|-----------| 1 | 3 | 0 | 3 | | 1.000 | 0.000 | 0.375 | -------------|-----------|-----------|-----------| 2 | 0 | 5 | 5 | | 0.000 | 1.000 | 0.625 | -------------|-----------|-----------|-----------| Column Total | 3 | 5 | 8 | -------------|-----------|-----------|-----------| This gives you the proportions in the row margins versus the columns. You still end up with some extraneous data, but perhaps a little less so. Just seems like overkill to get the same information that:> prop.table(table(sex))sex 1 2 0.375 0.625 gives you, without the lengthy function call. :-) Thanks Gabor. Best regards, Marc
On Sun, 2004-02-29 at 19:26, Gabor Grothendieck wrote:> I agree its overkill but prop.table generalizes to mulitple > dimensions, in which case it becomes comparable to CrossTable, > so I thought it was worth mentioning. > > By the way, as the author of CrossTable, perhaps you > might might consider generalizing CrossTable to the 1D > case too?As a result of this exchange, it is now on my todo list... :-) Thanks Gabor. Marc