tally: Create Count or Proportion Cross-tabulations with Optional...

Description Usage Arguments Details Value Author(s) Examples

Description

Creates an n-way table (cross-tabulation) with options to display results as weighted or unweighted counts, proportions or percentages, to include column, row or joint table totals and to control the number of decimal places used when reporting the output.

Usage

1
2
3
tally(formula=NULL, format="count", data=NULL, na.rm=FALSE,
      cumsum=FALSE, margin="none",
      dp=NULL, weights=NULL, flat=FALSE)

Arguments

formula

A formula specifying the variable(s) to be included in the cross-tabulation. (See details.)

format

A character string specifying the type of output required. The default is “count”. Other options are “proportion” and “percentage”.

data

A dataframe containing the variables to be cross-tabulated.

na.rm

A logical value (TRUE/FALSE) that specifies whether or not to remove missing values from the reported cross-tabulation. The default value is FALSE.

cumsum

A logical value (TRUE/FALSE) that specifies whether the cell values should be cumulated. The default value is FALSE. The way in which values are cumulated (across rows or down columns) is determined by margin.

margin

A character string specifying what type of table margins are required. The default value is “none”. Other options are “row”, “col” and “joint”.

dp

Integer. The number of decimal places to which results will be reported. The default is NULL, which rounds percentages, proportions and weights counts to 2 d.p., but leaves unweighted counts unrounded.

weights

The name of a variable in the supplied dataframe to be used to weight the table. The default is NULL (no weight).

flat

A logical value indicating whether to return the table in ‘flat’ (TRUE) or ‘list’(FALSE) format. The default value is FALSE (return table in list format).

Details

For univariate distributions, only include a variable name on the right-hand side of the the formula (e.g. ~ Tenure). For bivariate distributions include a variable on both sides of the formula (e.g. Health ~ Tenure). The variable on the left defines the rows; the variable on the right defines the columns. For higher dimensional distributions, include additional variables on the right-hand side of the formula (e.g. Health ~ Tenure + Sex).

Value

Returned as a table data object, unless flat = TRUE, in which case a ftable data object is returned.

Author(s)

Paul Williamson

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
## n-way frequency counts
tally( ~Tenure, data=survey)
tally(Health ~ Tenure, data=survey)
tally(Health ~ Tenure + Sex, data=survey)

## Flat 3-way table
tally(Health ~ Tenure + Sex, data=survey, flat=TRUE)

## Add row/column/joint totals
tally(Health ~ Tenure, data=survey, margin = "row")
tally(Health ~ Tenure, data=survey, margin = "col")
tally(Health ~ Tenure, data=survey, margin = "joint")

## Report as percentages or proportions
tally(Health ~ Tenure, data=survey, margin = "col", format = "percent")
tally(Health ~ Tenure, data=survey, margin = "col", format = "proportion")

## Cumulative
tally(Health ~ Tenure, data=survey, margin = "col",
      format = "percent", cumsum=TRUE)

## Weighted
tally(Health ~ Tenure, data=survey, margin = "col",
      format = "percent", cumsum=TRUE, weight="Person_w0")

## Specify decimal places in reported output
tally(Health ~ Tenure, data=survey, margin = "col",
      format = "percent", dp=4)

DrPaulWilliamson/ENVS450 documentation built on May 9, 2019, 3:23 a.m.