Return the percentages of a cross-tabulation table (2 dimensions or more) with formatting and printing options.
prop(tab, ...)
prop_table(
tab,
digits = 1,
total = TRUE,
percent = FALSE,
drop = TRUE,
n = FALSE,
...
)
# S3 method for class 'data.frame'
prop(
tab,
digits = 1,
total = TRUE,
percent = FALSE,
drop = TRUE,
n = FALSE,
...
)
# S3 method for class 'matrix'
prop(
tab,
digits = 1,
total = TRUE,
percent = FALSE,
drop = TRUE,
n = FALSE,
...
)
# S3 method for class 'tabyl'
prop(tab, digits = 1, total = TRUE, percent = FALSE, n = FALSE, ...)
frequency table
parameters passed to other methods
number of digits to display
if TRUE
, add a column with the sum of percentages and a row with global percentages
if TRUE
, add a percent sign after the values when printing
if TRUE
, lines or columns with a sum of zero, which would generate NaN
percentages, are dropped. Unused for tables of 3 dimensions or more (always `FALSE`).
if TRUE
, display number of observations per row and per column.
The result is an object of class table
and proptab
.
## 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
## Could be applied to a table of 3 dimensions or more
prop(Titanic)
#> , , Age = Child, Survived = No
#>
#> Sex
#> Class Male Female Total
#> 1st 0.0 0.0 0.0
#> 2nd 0.0 0.0 0.0
#> 3rd 67.3 32.7 100.0
#> Crew 0.0 0.0 0.0
#> Total 67.3 32.7 100.0
#>
#> , , Age = Adult, Survived = No
#>
#> Sex
#> Class Male Female Total
#> 1st 8.2 0.3 8.5
#> 2nd 10.7 0.9 11.6
#> 3rd 26.9 6.2 33.1
#> Crew 46.6 0.2 46.8
#> Total 92.4 7.6 100.0
#>
#> , , Age = Child, Survived = Yes
#>
#> Sex
#> Class Male Female Total
#> 1st 8.8 1.8 10.5
#> 2nd 19.3 22.8 42.1
#> 3rd 22.8 24.6 47.4
#> Crew 0.0 0.0 0.0
#> Total 50.9 49.1 100.0
#>
#> , , Age = Adult, Survived = Yes
#>
#> Sex
#> Class Male Female Total
#> 1st 8.7 21.4 30.1
#> 2nd 2.1 12.2 14.4
#> 3rd 11.5 11.6 23.1
#> Crew 29.4 3.1 32.4
#> Total 51.7 48.3 100.0
#>