[R] Frequencies, proportions & cumulative proportions

(Ted Harding) Ted.Harding at manchester.ac.uk
Fri Oct 16 23:48:09 CEST 2009


On 16-Oct-09 20:51:06, Muenchen, Robert A (Bob) wrote:
> Dear R-Helpers,
> 
> I've looked high and low for a function that provides frequencies,
> proportions and cumulative proportions side-by-side. Below is the table
> I need. Is there a function that already does it?
> 
> Thanks,
> Bob
> 
>> # Generate some test scores
>> myValues <- c(70:95)
>> Score <- ( sample( myValues, size=1000, replace=TRUE) )
>> head(Score)
> [1] 77 71 81 88 83 93
>> 
>> # Get frequencies & proportions
>> myTable <- data.frame( table(Score) )
>> myTable$Prop <- prop.table( myTable$Freq )
>> myTable$CumProp <-  cumsum( myTable$Prop )
>> 
>> # Print result
>> myTable
>    Score Freq  Prop CumProp
> 1     70   44 0.044   0.044
> 2     71   42 0.042   0.086
> 3     72   40 0.040   0.126
> 4     73   40 0.040   0.166
> 5     74   43 0.043   0.209
> 6     75   45 0.045   0.254
> 7     76   46 0.046   0.300
> 8     77   40 0.040   0.340
> 9     78   46 0.046   0.386
> 10    79   43 0.043   0.429
> 11    80   37 0.037   0.466
> 12    81   29 0.029   0.495
> 13    82   33 0.033   0.528
> 14    83   39 0.039   0.567
> 15    84   31 0.031   0.598
> 16    85   32 0.032   0.630
> 17    86   31 0.031   0.661
> 18    87   37 0.037   0.698
> 19    88   30 0.030   0.728
> 20    89   33 0.033   0.761
> 21    90   43 0.043   0.804
> 22    91   41 0.041   0.845
> 23    92   37 0.037   0.882
> 24    93   39 0.039   0.921
> 25    94   42 0.042   0.963
> 26    95   37 0.037   1.000

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.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 16-Oct-09                                       Time: 22:48:06
------------------------------ XFMail ------------------------------




More information about the R-help mailing list