Return the percentages of a two-way frequency table with formatting and printing options.

prop(tab, ...)

prop_table(
  tab,
  digits = 1,
  total = TRUE,
  percent = FALSE,
  drop = TRUE,
  n = FALSE,
  ...
)

# S3 method for data.frame
prop(
  tab,
  digits = 1,
  total = TRUE,
  percent = FALSE,
  drop = TRUE,
  n = FALSE,
  ...
)

# S3 method for matrix
prop(
  tab,
  digits = 1,
  total = TRUE,
  percent = FALSE,
  drop = TRUE,
  n = FALSE,
  ...
)

# S3 method for tabyl
prop(tab, digits = 1, total = TRUE, percent = FALSE, n = FALSE, ...)

Arguments

tab

frequency table

...

parameters passed to other methods

digits

number of digits to display

total

if TRUE, add a column with the sum of percentages and a row with global percentages

percent

if TRUE, add a percent sign after the values when printing

drop

if TRUE, lines or columns with a sum of zero, which would generate NaN percentages, are dropped.

n

if TRUE, display number of observations per row and per column.

Value

The result is an object of class table and proptab.

See also

Examples

## Sample table
data(Titanic)
tab <- apply(Titanic, c(1,4), sum)
## Percentages
prop(tab)
#>        Survived
#> Class   No    Yes   Total
#>   1st     5.5   9.2  14.8
#>   2nd     7.6   5.4  12.9
#>   3rd    24.0   8.1  32.1
#>   Crew   30.6   9.6  40.2
#>   Total  67.7  32.3 100.0
## Percentages with custom display
prop(tab, digits=2, percent=TRUE, total=FALSE, n=TRUE)
#>       Survived
#> Class  No       Yes      n   
#>   1st     5.54%    9.22%  325
#>   2nd     7.59%    5.36%  285
#>   3rd    23.99%    8.09%  706
#>   Crew   30.58%    9.63%  885
#>   n    1490      711     2201