Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

percentages

Easy Creation of Tables of Percentages


Description

The generic function percentages and its methods create one- or multidimensional tables of percentages. As such, the function percentages can be viewed as a convenience interface to prop.table. However, it also allows to obtain standard errors and confidence intervals.

Usage

percentages(obj, ...)
## S3 method for class 'table'
percentages(obj,
      by=NULL, which=NULL, se=FALSE, ci=FALSE, ci.level=.95, ...)
## S3 method for class 'formula'
percentages(obj,
      data=parent.frame(), weights=NULL, ...)
## S3 method for class 'percentage.table'
as.data.frame(x, ...)
## S3 method for class 'xpercentage.table'
as.data.frame(x, ...)

Arguments

obj

an object; a contingency table or a formula. If it is a formula, its left-hand side determines the factor or combination of factors for which percentages are computed while its right-hand side determines the factor or combination of factors that define the groups within which percentages are computed.

by

a character vector with the names of the factor variables that define the groups within which percentages are computed. Percentages sum to 100 within combination of levels of these factors.

which

a character vector with the names of the factor variables for which percentages are computed.

se

a logical value; determines whether standard errors are computed.

ci

a logical value; determines whether confidence intervals are computed. Note that the confidence intervals are for infinite (or very large) populations.

ci.level

a numerical value, the required confidence level of the confidence intervals.

data

a contingency table (an object that inherits from "table") or a data frame or an object coercable into a data frame.

weights

an optional vector of weights. Should be NULL or a numeric vector.

x

an object coerced into a data frame.

...

Further arguments, ignored.

Value

An array that inherits classes "percentage.table" and "table". If percentages was called with se=TRUE or ci=TRUE then the result additionally inherits class "xpercentage.table".

Examples

# Two equivalent ways to create the same table
100*prop.table(UCBAdmissions)
percentages(UCBAdmissions)

# Three equivalent ways to create the same table
100*prop.table(UCBAdmissions,c(2,3))
(p0 <- percentages(UCBAdmissions,by=c("Gender","Dept")))
percentages(UCBAdmissions,which="Admit")
# Percentage table as data frame
as.data.frame(p0)

# Three equivalent ways to create the same table
100*prop.table(margin.table(UCBAdmissions,c(1,2)),2)
percentages(UCBAdmissions,which="Admit",by="Gender")
percentages(Admit~Gender,data=UCBAdmissions)

# Three equivalent ways to create the same table
100*prop.table(margin.table(UCBAdmissions,c(1,3)),2)
percentages(Admit~Dept,data=UCBAdmissions)
percentages(Admit~Dept,data=as.data.frame(UCBAdmissions),
            weights=Freq)

# Standard errors and confidence intervals
percentages(Admit~Dept,data=UCBAdmissions,se=TRUE)
percentages(Admit~Dept,data=UCBAdmissions,ci=TRUE)
(p<- percentages(Admit~Dept,data=UCBAdmissions,ci=TRUE,se=TRUE))

# An extended table of percentages as data frame
as.data.frame(p)

memisc

Management of Survey Data and Presentation of Analysis Results

v0.99.27.3
GPL-2
Authors
Martin Elff (with contributions from Christopher N. Lawrence, Dave Atkins, Jason W. Morgan, Achim Zeileis)
Initial release
2020-11-18

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.