Last data update: 2014.03.03

R: Cross Tabulation
crossTableR Documentation

Cross Tabulation

Description

Output well-formatted cross tabulation. Also can genarate latex syntax of cross tabulation.

Usage

crossTable(..., deparse.level = 2)
## S3 method for class 'CrossTable'
summary(object, digits=3, latex=FALSE, ...)

Arguments

deparse.level

passed to table

...

passed to table

object

crossTable object

digits

integer, used for number formatting

latex

logical, generate latex syntax if TRUE

Value

crossTable returns a object which belongs to CrossTable class and table class.

Author(s)

Masahiro Hayashi

Examples

sex    <- factor(rbinom(1:1000, 1, 0.5), labels=c("male" , "female"))
age    <- factor(rbinom(1:1000, 2, 0.4), labels=c("young", "middle", "old"))
weight <- factor(rbinom(1:1000, 2, 0.6), labels=c("light", "middle", "heavy"))

cross.table1 <- crossTable(sex, age)
summary(cross.table1)

cross.table2 <- crossTable(sex, age, weight)
summary(cross.table2)

summary(cross.table2, latex = TRUE)

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(Rz)
Loading required package: grid
Loading required package: foreign
Loading required package: memisc
Loading required package: lattice
Loading required package: MASS

Attaching package: 'memisc'

The following objects are masked from 'package:stats':

    contr.sum, contr.treatment, contrasts

The following object is masked from 'package:base':

    as.array

Loading required package: psych
Loading required package: ggplot2

Attaching package: 'ggplot2'

The following objects are masked from 'package:psych':

    %+%, alpha

################################ Rz ################################
                       Excute Rz() to start,                        
      or you can start from menu bar if you use R on Windows.       
####################################################################
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/Rz/crossTable.Rd_%03d_medium.png", width=480, height=480)
> ### Name: crossTable
> ### Title: Cross Tabulation
> ### Aliases: crossTable summary.CrossTable
> ### Keywords: cross tabulation
> 
> ### ** Examples
> 
> sex    <- factor(rbinom(1:1000, 1, 0.5), labels=c("male" , "female"))
> age    <- factor(rbinom(1:1000, 2, 0.4), labels=c("young", "middle", "old"))
> weight <- factor(rbinom(1:1000, 2, 0.6), labels=c("light", "middle", "heavy"))
> 
> cross.table1 <- crossTable(sex, age)
> summary(cross.table1)
==================================
               age                
       --------------------       
sex    young  middle  old   Total 
----------------------------------
male      187    247     76    510
        36.7%  48.4%  14.9%   100%
female    173    248     69    490
        35.3%  50.6%  14.1%   100%
----------------------------------
Total     360    495    145   1000
        36.0%  49.5%  14.5%   100%
==================================

Chi-Square Test for Independence

Number of cases in table: 1000 
Number of factors: 2 
Test for independence of all factors:
	Chisq = 0.4846, df = 2, p-value = 0.7848
                     X^2 df P(> X^2)
Likelihood Ratio 0.48464  2  0.78481
Pearson          0.48459  2  0.78482

Phi-Coefficient   : NA 
Contingency Coeff.: 0.022 
Cramer's V        : 0.022 

> 
> cross.table2 <- crossTable(sex, age, weight)
> summary(cross.table2)
=========================================
                       weight              
                --------------------       
sex    age    light  middle heavy  Total 
-----------------------------------------
male   young      27     97     63    187
              14.44%  51.9%  33.7%   100%
       middle     39    115     93    247
              15.79%  46.6%  37.7%   100%
       old         5     44     27     76
               6.58%  57.9%  35.5%   100%
       ----------------------------------
       Total      71    256    183    510
              13.92%  50.2%  35.9%   100%
-----------------------------------------
female young      30     84     59    173
               17.3%  48.6%  34.1%   100%
       middle     46    126     76    248
               18.5%  50.8%  30.6%   100%
       old         9     32     28     69
               13.0%  46.4%  40.6%   100%
       ----------------------------------
       Total      85    242    163    490
               17.3%  49.4%  33.3%   100%
-----------------------------------------
Total  young      57    181    122    360
              15.83%  50.3%  33.9%   100%
       middle     85    241    169    495
              17.17%  48.7%  34.1%   100%
       old        14     76     55    145
               9.66%  52.4%  37.9%   100%
       ----------------------------------
       Total     156    498    346   1000
              15.60%  49.8%  34.6%   100%
=========================================

Chi-Square Test for Independence

sex : male  

Number of cases in table: 510 
Number of factors: 2 
Test for independence of all factors:
	Chisq = 5.72, df = 4, p-value = 0.2211
                    X^2 df P(> X^2)
Likelihood Ratio 6.3897  4  0.17187
Pearson          5.7199  4  0.22106

Phi-Coefficient   : NA 
Contingency Coeff.: 0.105 
Cramer's V        : 0.075 


sex : female

Number of cases in table: 490 
Number of factors: 2 
Test for independence of all factors:
	Chisq = 2.8532, df = 4, p-value = 0.5827
                    X^2 df P(> X^2)
Likelihood Ratio 2.8605  4  0.58144
Pearson          2.8532  4  0.58269

Phi-Coefficient   : NA 
Contingency Coeff.: 0.076 
Cramer's V        : 0.054 


Total

Number of cases in table: 1000 
Number of factors: 2 
Test for independence of all factors:
	Chisq = 4.968, df = 4, p-value = 0.2906
                    X^2 df P(> X^2)
Likelihood Ratio 5.4475  4  0.24438
Pearson          4.9676  4  0.29064

Phi-Coefficient   : NA 
Contingency Coeff.: 0.07 
Cramer's V        : 0.05 


> 
> summary(cross.table2, latex = TRUE)
egin{table}[htbp]
centering
caption{sex $\times$ age $\times$ weight}
egin{tabular}{llrrrr}
\toprule
        &      &                           multicolumn{3}{c}{weight}                           &                           \
        cline{3-5}
sex   &age   &multicolumn{1}{c}{light}&multicolumn{1}{c}{middle}&multicolumn{1}{c}{heavy}&multicolumn{1}{c}{Total} \
midrule
male  &young &               27         &               97          &              63          &             187           \
      &      &         14.44%         &          51.9%          &         33.7%          &          100%           \
      &middle&               39         &              115          &              93          &             247           \
      &      &         15.79%         &          46.6%          &         37.7%          &          100%           \
      &old   &                5         &               44          &              27          &              76           \
      &      &          6.58%         &          57.9%          &         35.5%          &          100%           \
      midrule
      &Total &               71         &              256          &             183          &             510           \
      &      &         13.92%         &          50.2%          &         35.9%          &          100%           \
midrule
female&young &              30          &               84          &              59          &             173           \
      &      &         17.3%          &          48.6%          &         34.1%          &          100%           \
      &middle&              46          &              126          &              76          &             248           \
      &      &         18.5%          &          50.8%          &         30.6%          &          100%           \
      &old   &               9          &               32          &              28          &              69           \
      &      &         13.0%          &          46.4%          &         40.6%          &          100%           \
      midrule
      &Total &              85          &              242          &             163          &             490           \
      &      &         17.3%          &          49.4%          &         33.3%          &          100%           \
midrule
Total &young &               57         &              181          &             122          &             360           \
      &      &         15.83%         &          50.3%          &         33.9%          &          100%           \
      &middle&               85         &              241          &             169          &             495           \
      &      &         17.17%         &          48.7%          &         34.1%          &          100%           \
      &old   &               14         &               76          &              55          &             145           \
      &      &          9.66%         &          52.4%          &         37.9%          &          100%           \
      midrule
      &Total &              156         &              498          &             346          &            1000           \
      &      &         15.60%         &          49.8%          &         34.6%          &          100%           \
ottomrule
end{tabular}
end{table}

Chi-Square Test for Independence

sex : male  

Number of cases in table: 510 
Number of factors: 2 
Test for independence of all factors:
	Chisq = 5.72, df = 4, p-value = 0.2211
                    X^2 df P(> X^2)
Likelihood Ratio 6.3897  4  0.17187
Pearson          5.7199  4  0.22106

Phi-Coefficient   : NA 
Contingency Coeff.: 0.105 
Cramer's V        : 0.075 


sex : female

Number of cases in table: 490 
Number of factors: 2 
Test for independence of all factors:
	Chisq = 2.8532, df = 4, p-value = 0.5827
                    X^2 df P(> X^2)
Likelihood Ratio 2.8605  4  0.58144
Pearson          2.8532  4  0.58269

Phi-Coefficient   : NA 
Contingency Coeff.: 0.076 
Cramer's V        : 0.054 


Total

Number of cases in table: 1000 
Number of factors: 2 
Test for independence of all factors:
	Chisq = 4.968, df = 4, p-value = 0.2906
                    X^2 df P(> X^2)
Likelihood Ratio 5.4475  4  0.24438
Pearson          4.9676  4  0.29064

Phi-Coefficient   : NA 
Contingency Coeff.: 0.07 
Cramer's V        : 0.05 


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