Last data update: 2014.03.03

R: Row, Column, and Total Percentage Tables
colPercentsR Documentation

Row, Column, and Total Percentage Tables

Description

Percentage a matrix or higher-dimensional array of frequency counts by rows, columns, or total frequency.

Usage

colPercents(tab, digits=1)
rowPercents(tab, digits=1)
totPercents(tab, digits=1)

Arguments

tab

a matrix or higher-dimensional array of frequency counts.

digits

number of places to the right of the decimal place for percentages.

Value

Returns an array of the same size and shape as tab percentaged by rows or columns, plus rows or columns of totals and counts, or by the table total.

Author(s)

John Fox jfox@mcmaster.ca

Examples

if (require(car)){
    data(Mroz) # from car package
    cat("\n\n column percents:\n")
    print(colPercents(xtabs(~ lfp + wc, data=Mroz)))
    cat("\n\n row percents:\n")
    print(rowPercents(xtabs(~ hc + lfp, data=Mroz)))
    cat("\n\n total percents:\n")
    print(totPercents(xtabs(~ hc + wc, data=Mroz)))
    cat("\n\n three-way table, column percents:\n")
    print(colPercents(xtabs(~ lfp + wc + hc, data=Mroz)))
}

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(RcmdrMisc)
Loading required package: car
Loading required package: sandwich
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/RcmdrMisc/colPercents.Rd_%03d_medium.png", width=480, height=480)
> ### Name: colPercents
> ### Title: Row, Column, and Total Percentage Tables
> ### Aliases: colPercents rowPercents totPercents
> ### Keywords: misc
> 
> ### ** Examples
> 
> if (require(car)){
+     data(Mroz) # from car package
+     cat("\n\n column percents:\n")
+     print(colPercents(xtabs(~ lfp + wc, data=Mroz)))
+     cat("\n\n row percents:\n")
+     print(rowPercents(xtabs(~ hc + lfp, data=Mroz)))
+     cat("\n\n total percents:\n")
+     print(totPercents(xtabs(~ hc + wc, data=Mroz)))
+     cat("\n\n three-way table, column percents:\n")
+     print(colPercents(xtabs(~ lfp + wc + hc, data=Mroz)))
+ }


 column percents:
       wc
lfp        no   yes
  no     47.5  32.1
  yes    52.5  67.9
  Total 100.0 100.0
  Count 541.0 212.0


 row percents:
     lfp
hc      no  yes Total Count
  no  45.2 54.8   100   458
  yes 40.0 60.0   100   295


 total percents:
        no  yes Total
no    55.4  5.4  60.8
yes   16.5 22.7  39.2
Total 71.8 28.2 100.0


 three-way table, column percents:
, , hc = no

       wc
lfp        no yes
  no     47.5  22
  yes    52.5  78
  Total 100.0 100
  Count 417.0  41

, , hc = yes

       wc
lfp        no   yes
  no     47.6  34.5
  yes    52.4  65.5
  Total 100.0 100.0
  Count 124.0 171.0

> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>