Transforms a (possibly weighted) contingency table into percentages

Share:

Description

Computes a contingency table from one or two vectors, with the possibility of specifying weights, and then computes the percentages.

Usage

1
prop.wtable(var1,var2=NULL,w=rep.int(1,length(var1)),dir=0,digits=1,mar=TRUE,na=TRUE)

Arguments

var1

an object which can be interpreted as factor

var2

an optional object which can be interpreted as factor

w

an optional numeric vector of weights (by default, a vector of 1 for uniform weights)

dir

integer: 0 for percentages, 1 for row percentages and 2 for column percentages (default is 0)

digits

integer indicating the number of decimal places (default is 1)

mar

logical. If TRUE (default), margins are computed

na

logical. If TRUE (default), 'NA' are treated as a category. If FALSE, they are ignored

Value

Returns a contingency table expressed in percentages in matrix format.

Author(s)

Nicolas Robette

See Also

wtable, prop.table

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
## Computes a contingency table
## (expressed in percentages)
## of jazz and age variables
## from the 'Music' example data set
## with or without weights
data(Music)
prop.wtable(Music$Jazz)
prop.wtable(Music$Jazz,Music$Gender)
prop.wtable(Music$Jazz,Music$Gender,dir=1)
prop.wtable(Music$Jazz,Music$Gender,dir=2)

weight <- sample(0:20,nrow(Music),TRUE)/10
prop.wtable(Music$Jazz,w=weight)
prop.wtable(Music$Jazz,Music$Age,weight)
prop.wtable(Music$Jazz,Music$Age,weight,dir=1)
prop.wtable(Music$Jazz,Music$Age,weight,dir=2)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.