Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

xtab

Crosstabulate variables


Description

Crosstabulates variables with small numbers of unique values.

Usage

xtab(formula,data,varnames=NULL,or=TRUE,chisq=FALSE,phi=FALSE,html=FALSE,
  bgcol="lightgray",lastone=TRUE)

Arguments

formula

a formula containing the variables to be crosstabulated

data

the data frame from which to select the variables

varnames

optional labels for the variables (defaults to names(data))

or

whether to calculate the odds ratio (only for 2x2 tables).

chisq

logical - whether to display chi squared test(s) of the table(s)

phi

whether to calculate and display the phi coefficient of association - only for 2x2 tables

html

whether to format the resulting table with HTML tags.

bgcol

background color for the table if html=TRUE.

lastone

A flag that controls the names of the returned list.

Details

xtab will accept a formula referring to columns in a data frame or two explicit variable names. It calls calculate.xtab for the calculations and displays one or more tables of results by calling print.xtab. If html is TRUE, the resulting table will be formatted with HTML tags. The default value of lastone should not be changed.

Value

The result of calculate.xtab if there is only one table to display, otherwise a nested list of tables.

Author(s)

Jim Lemon

See Also

Examples

test.df<-data.frame(sex=sample(c("MALE","FEMALE"),1000,TRUE),
 suburb=sample(1:4,1000,TRUE),social.type=sample(LETTERS[1:4],1000,TRUE))
 xtab(sex~suburb+social.type,test.df,chisq=TRUE)
 # now add some value labels
 attr(test.df$suburb,"value.labels")<-1:4
 names(attr(test.df$suburb,"value.labels"))<-
  c("Upper","Middle","Working","Slum")
 attr(test.df$social.type,"value.labels")<-LETTERS[1:4]
 names(attr(test.df$social.type,"value.labels"))<-
  c("Gregarious","Mixer","Aloof","Hermit")
 xtab(sex~suburb+social.type,test.df)
 # now have some fun with ridiculously long factor labels
 testxtab<-data.frame(firstbit=sample(c("Ecomaniacs","Redneck rogues"),50,TRUE),
  secondbit=sample(c("Macho bungy jumpers","Wimpy quiche munchers"),50,TRUE))
 # and format the table in HTML and add some tests
 xtab(secondbit~firstbit,testxtab,html=TRUE,chisq=TRUE,phi=TRUE)

prettyR

Pretty Descriptive Stats

v2.2-3
GPL (>= 2)
Authors
Jim Lemon <drjimlemon@gmail.com>, Philippe Grosjean <phgrosjean@sciviews.org>
Initial release
2019-04-08

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.