[R] Frequencies, proportions & cumulative proportions

Muenchen, Robert A (Bob) muenchen at utk.edu
Sat Oct 17 15:15:21 CEST 2009


David,

I use CrossTable, so that was my first guess. It'll do
proportions/percents by row, column or total in a 2-way table. For 1-way
tables, it still tries looks like a 2-way table, unless you specify
max.width=1. Then it does one column, but no cumulative proportions (see
below).

I appreciate the idea though!

Thanks,
Bob

> CrossTable(Score, max.width=1)

 
   Cell Contents
|-------------------------|
|                       N |
|         N / Table Total |
|-------------------------|

 
Total Observations in Table:  1000 

 
          |        70 | 
          |-----------|
          |        44 | 
          |     0.044 | 
          |-----------|


          |        71 | 
          |-----------|
          |        42 | 
          |     0.042 | 
          |-----------|


          |        72 | 
          |-----------|
          |        40 | 
          |     0.040 | 
          |-----------|


          |        73 | 
          |-----------|
          |        40 | 
          |     0.040 | 
          |-----------|


          |        74 | 
          |-----------|
          |        43 | 
          |     0.043 | 
          |-----------|


          |        75 | 
          |-----------|
          |        45 | 
          |     0.045 | 
          |-----------|


          |        76 | 
          |-----------|
          |        46 | 
          |     0.046 | 
          |-----------|


          |        77 | 
          |-----------|
          |        40 | 
          |     0.040 | 
          |-----------|


          |        78 | 
          |-----------|
          |        46 | 
          |     0.046 | 
          |-----------|


          |        79 | 
          |-----------|
          |        43 | 
          |     0.043 | 
          |-----------|

...

-----Original Message-----
From: David Scott [mailto:d.scott at auckland.ac.nz] 
Sent: Friday, October 16, 2009 8:42 PM
To: Muenchen, Robert A (Bob)
Cc: ted.harding at manchester.ac.uk; r-help at r-project.org
Subject: Re: [R] Frequencies, proportions & cumulative proportions

Muenchen, Robert A (Bob) wrote:
> Ted,
> 
> I know how to do that. It's just such a standard display in SAS, SPSS
> and Stata that I figured someone had done it and I had just overlooked
> it.
> 
> Thanks!
> Bob
> 
> 
> 
> I don't think there is a ready-made one, but it is very little
> effort to make your own:
> 
> mkMyTable <- function(X){
>   Table <- data.frame( table(X) )
>   Table$Prop <- prop.table( Table$Freq )
>   Table$CumProp <-  cumsum( Table$Prop )
>   Table
> }
> 
> myTable <- mkMyTable(Score)
> 
> Hoping this helps!
> Ted.
> 
I think CrossTable in gmodels does what Bob is after:

CrossTable(gmodels) R Documentation

Cross Tabulation with Tests for Factor Independence
Description
An implementation of a cross-tabulation function with output similar to 
S-Plus crosstabs() and SAS Proc Freq (or SPSS format) with Chi-square, 
Fisher and McNemar tests of the independence of all table factors.



David Scott

-- 
_________________________________________________________________
David Scott	Department of Statistics
		The University of Auckland, PB 92019
		Auckland 1142,    NEW ZEALAND
Phone: +64 9 923 5055, or +64 9 373 7599 ext 85055
Email:	d.scott at auckland.ac.nz,  Fax: +64 9 373 7018

Director of Consulting, Department of Statistics




More information about the R-help mailing list