Return the column percentages of a cross-tabulation table (2 dimensions or more) with formatting and printing options.
cprop(tab, ...)
# S3 method for class 'table'
cprop(
tab,
digits = 1,
total = TRUE,
percent = FALSE,
drop = TRUE,
n = FALSE,
...
)
# S3 method for class 'data.frame'
cprop(
tab,
digits = 1,
total = TRUE,
percent = FALSE,
drop = TRUE,
n = FALSE,
...
)
# S3 method for class 'matrix'
cprop(
tab,
digits = 1,
total = TRUE,
percent = FALSE,
drop = TRUE,
n = FALSE,
...
)
# S3 method for class 'tabyl'
cprop(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 row with the sum of percentages and a column 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 column.
The result is an object of class table
and proptab
.
rprop
, prop
, table
, prop.table
## Sample table
data(Titanic)
tab <- apply(Titanic, c(4,1), sum)
## Column percentages
cprop(tab)
#> Class
#> Survived 1st 2nd 3rd Crew All
#> No 37.5 58.6 74.8 76.0 67.7
#> Yes 62.5 41.4 25.2 24.0 32.3
#> Total 100.0 100.0 100.0 100.0 100.0
## Column percentages with custom display
cprop(tab, digits=2, percent=TRUE, total=FALSE)
#> Class
#> Survived 1st 2nd 3rd Crew
#> No 37.54% 58.60% 74.79% 76.05%
#> Yes 62.46% 41.40% 25.21% 23.95%
## Could be applied to a table of 3 dimensions or more
cprop(Titanic)
#> , , Age = Child, Survived = No
#>
#> Sex
#> Class Male Female All
#> 1st 0.0 0.0 0.0
#> 2nd 0.0 0.0 0.0
#> 3rd 100.0 100.0 100.0
#> Crew 0.0 0.0 0.0
#> Total 100.0 100.0 100.0
#>
#> , , Age = Adult, Survived = No
#>
#> Sex
#> Class Male Female All
#> 1st 8.9 3.7 8.5
#> 2nd 11.6 11.9 11.6
#> 3rd 29.1 81.7 33.1
#> Crew 50.4 2.8 46.8
#> Total 100.0 100.0 100.0
#>
#> , , Age = Child, Survived = Yes
#>
#> Sex
#> Class Male Female All
#> 1st 17.2 3.6 10.5
#> 2nd 37.9 46.4 42.1
#> 3rd 44.8 50.0 47.4
#> Crew 0.0 0.0 0.0
#> Total 100.0 100.0 100.0
#>
#> , , Age = Adult, Survived = Yes
#>
#> Sex
#> Class Male Female All
#> 1st 16.9 44.3 30.1
#> 2nd 4.1 25.3 14.4
#> 3rd 22.2 24.1 23.1
#> Crew 56.8 6.3 32.4
#> Total 100.0 100.0 100.0
#>