Table of Percentages with Percentage Base

Description

percent returns a table of percentages along with the percentage base. It will be useful in conjunction with Aggregate or genTable.

Usage

  percent(x,...)
  ## S4 method for signature 'default'
percent(x,weights=NULL,total=!(se || ci),
      se=FALSE,ci=FALSE,ci.level=.95,
      total.name="N",perc.label="Percentage",...)
  ## S4 method for signature 'logical'
percent(x,weights=NULL,total=!(se || ci),
      se=FALSE,ci=FALSE,ci.level=.95,
      total.name="N",perc.label="Percentage",...)

Arguments

x

a numeric vector or factor.

weights

a optional numeric vector of weights of the same length as x.

total

logical; should the total sum of counts from which the percentages are computed be included into the output?

se

logical; should standard errors of the percentages be included?

ci

logical; should confidence intervals of the percentages be included?

ci.level

numeric; nominal coverage of confidence intervals

total.name

character; name given for the total sum of counts

perc.label

character; label given for the percentages if the table has more than one dimensions, e.g. if se or ci is TRUE.

...

for percent.mresp: one or several 1-0 vectors or matrices otherwise, further arguments, currently ignored.

Value

A table of percentages.

Examples

x <- rnorm(100)
y <- rnorm(100)
z <- rnorm(100)
f <- sample(1:3,100,replace=TRUE)
f <- factor(f,labels=c("a","b","c"))
percent(x>0)
Percentage          N
        43        100
percent(f)
 a   b   c   N
35  35  30 100
genTable(
 cbind(percent(x>0),
       percent(y>0),
       percent(z>0)) ~ f
 )
, , f = a

                    1        2        3
  Percentage 42.85714 51.42857 42.85714
  N          35.00000 35.00000 35.00000

, , f = b

                    1        2        3
  Percentage 31.42857 42.85714 48.57143
  N          35.00000 35.00000 35.00000

, , f = c

                    1        2        3
  Percentage 56.66667 63.33333 56.66667
  N          30.00000 30.00000 30.00000
gt <- genTable(
 cbind(percent(x>0,ci=TRUE),
       percent(y>0,ci=TRUE),
       percent(z>0,ci=TRUE)) ~ f
 )
print(gt)
, , f = a

                    1        2        3
  Percentage 42.85714 51.42857 42.85714
  lower      26.32272 33.98914 26.32272
  upper      60.64691 68.61715 60.64691

, , f = b

                    1        2        3
  Percentage 31.42857 42.85714 48.57143
  lower      16.85172 26.32272 31.38285
  upper      49.28800 60.64691 66.01086

, , f = c

                    1        2        3
  Percentage 56.66667 63.33333 56.66667
  lower      37.42735 43.85598 37.42735
  upper      74.53925 80.07014 74.53925
ftable(gt,row.vars=2,col.vars=c(3,1))
f a b c
Percentage lower upper Percentage lower upper Percentage lower upper

1 42.85714 26.32272 60.64691 31.42857 16.85172 49.28800 56.66667 37.42735
  74.53925
2 51.42857 33.98914 68.61715 42.85714 26.32272 60.64691 63.33333 43.85598
  80.07014
3 42.85714 26.32272 60.64691 48.57143 31.38285 66.01086 56.66667 37.42735
  74.53925
ex.data <- expand.grid(mean=c(0,25,50),sd=c(1,10,100))[rep(1:9,rep(250,9)),]
ex.data <- within(ex.data,x <- rnorm(n=nrow(ex.data),mean=ex.data$mean,sd=ex.data$sd))
ex.data <- within(ex.data,x.grp <- cases( x < 0,
                                           x >= 0 & x < 50,
                                           x >= 50 & x < 100,
                                           x >= 100
                                         ))
genTable(percent(x.grp)~mean+sd,data=ex.data)
, , sd = 1

                   mean
                        0    25    50
  x < 0              49.6   0.0   0.0
  x >= 0 & x < 50    50.4 100.0  49.6
  x >= 50 & x < 100   0.0   0.0  50.4
  x >= 100            0.0   0.0   0.0
  N                 250.0 250.0 250.0

, , sd = 10

                   mean
                        0    25    50
  x < 0              47.2   0.4   0.0
  x >= 0 & x < 50    52.8  99.2  50.4
  x >= 50 & x < 100   0.0   0.4  49.6
  x >= 100            0.0   0.0   0.0
  N                 250.0 250.0 250.0

, , sd = 100

                   mean
                        0    25    50
  x < 0              51.2  38.0  36.0
  x >= 0 & x < 50    17.2  23.2  19.6
  x >= 50 & x < 100  14.8  20.4  18.8
  x >= 100           16.8  18.4  25.6
  N                 250.0 250.0 250.0
Aggregate(percent(Admit,weight=Freq)~Gender+Dept,data=UCBAdmissions)
   Gender Dept  Admitted Rejected   N
1    Male    A 62.060606 37.93939 825
3  Female    A 82.407407 17.59259 108
5    Male    B 63.035714 36.96429 560
7  Female    B 68.000000 32.00000  25
9    Male    C 36.923077 63.07692 325
11 Female    C 34.064081 65.93592 593
13   Male    D 33.093525 66.90647 417
15 Female    D 34.933333 65.06667 375
17   Male    E 27.748691 72.25131 191
19 Female    E 23.918575 76.08142 393
21   Male    F  5.898123 94.10188 373
23 Female    F  7.038123 92.96188 341