Learn R Programming

catspec (version 0.97)

ctab: Percentage tables

Description

Produces one-way, two-way or multi-way percentage tables

Usage

ctab(...,dec.places=NULL,digits=NULL,type=NULL,style=NULL,row.vars=NULL,col.vars=NULL,percentages=NULL,addmargins=NULL)
"print"(x, dec.places=x$dec.places, addmargins=x$addmargins, ...)
"summary"(object,...)

Arguments

...
either
  • one or more factors,
  • a class table
  • a class ftable table object,
  • or a class ctab table object
dec.places
number of decimal places (default 2)
digits
synonym for dec.places, for compatability with previous version
type
Row, column, total percentages or counts (type= n). Multiple values may be specified as a character vector. Partial matchin is used.
style
Applicable if more than one percentage type is specified. If style=long, percentages are printed underneath each other. If style=wide, the percentages are printed side by side
row.vars
Same as ftable: “a vector of integers giving the numbers of the variables, or a character vector giving the names of the variables to be used for the rows of the [] table”
col.vars
“a vector of integers giving the numbers of the variables, or a character vector giving the names of the variables to be used for the columns of the [] table”
percentages
If FALSE, proportions rather than percentages are printed
addmargins
Use addmargins=TRUE to add subtotals to the table
x
is a tables object created by ctab
object
is a tables object created by ctab

Value

An object of class “ctab”. print.ctab prints the table, summary.ctab passes the frequency table on to summary.table, which prints the number of cases, number of factors, and a chi-square test of independence.
table
A class(table) object containing the table counts. Used by summary.ctab and by ctab itself if a ctab object is used as input.
ctab
A class(ftable) object containing the percentage types specified. This is printed by print.ctab.
row.vars
The row.vars options as numeric vectors
col.vars
The col.vars options as numeric vectors
dec.places
The dec.places option
type
The type option
style
The style option
percentages
The percentages option
addmargins
The addmargins option

Details

Options have default NULL so attributes of a ctab object can be used as default. In other cases, the following options will be used if nothing has been specified:
ctab(\dots, dec.places=2, digits=dec.places, type=c("n", "row", "column", "total"),
style="long", row.vars=NULL, col.vars=NULL, percentages=TRUE, addmargins=FALSE)

ctab uses ftable and prop.table to produce one-way frequency tables, two-way crosstables, or multi-way percentage tables. More than one percentage type may be specified, in which case “percentage type” is an unnamed dimension of the table. row.vars and col.vars can be used to control the layout of multi-way tables using the facilities of ftable. Subtotals can be added by specifiying addmargins=TRUE.

CrossTable in the gmodels package also provides an easy method for producing percentage tables, but is restricted to two-way tables.

If ctab is specified with no further options and for more than one factor, the output is identical to that of ftable. If a single factor is specified, the default is to print the frequencies column-wise with the percentaqes next to them.

References

http://home.wanadoo.nl/john.hendrickx/statres/

See Also

table, ftable, addmargins, prop.table, xtabs, [gmodels]CrossTable

Examples

Run this code
ftable(Titanic)
ctab(Titanic) # same output
ctab(Titanic,type="r")
ctab(Titanic,type=c("n","r"),addmargins=TRUE)
ctab(Titanic,type=c("n","c","t","r"),style="w")
mytab<-ftable(Titanic,row.vars=c(1,3),type="r")
mytab
ctab(mytab)
newtab<-ctab(mytab,type="r")
newtab
summary(newtab)

#second example using a data frame rather than table data
library(survival)
data(logan)
attach(logan)
class(logan) #"data.frame"
ctab(occupation)
ctab(occupation,addmargins=TRUE)
ctab(occupation,style="w",type="c")
ctab(occupation,style="l",type="n")
z<-ctab(occupation,addmargins=TRUE,style="l")
z
print(z,addmargins=FALSE,dec.places=5)
summary(z)

t<-ctab(focc,occupation,type=c("n","r","c"))
t
summary(t)

Run the code above in your browser using DataLab