How to Get a Contingency Table

Contingency table when you have a column with count values

We can use xtabs in base R

xtabs(Count ~ Year + Var, df1)
# Var
#Year A B
# 2019 10 36
# 2020 42 23

To include the row/column totals, can use addmargins

addmargins(xtabs(Count ~ Year + Var, df1))
# Var
#Year A B Sum
# 2019 10 36 46
# 2020 42 23 65
# Sum 52 59 111

data

df1 <- structure(list(Year = c(2019L, 2020L, 2019L, 2020L), Var = c("A", 
"B", "B", "A"), Count = c(10L, 23L, 36L, 42L)), class = "data.frame",
row.names = c(NA,
-4L))

Get contingency table for each column of a matrix

I would do this for something in general. For example

  • when you have a matrix of letters;
  • when you still have a matrix of integer but they are not contiguous, say, 5, 7, 10, 11, 20.

--

cX <- c(X)
k <- sort(unique(cX))
## as if we have a matrix of factors
XX <- matrix(match(cX, k), dim(X)[1], dimnames = list(k, 1:dim(X)[2]))
## aligned column-wise contingency table
tab <- apply(XX, 2, tabulate)
## aligned column-wise proportion table
prop <- tab / colSums(tab)[col(tab)]

I have abandoned my initial answer

lapply(data.frame(X), table)
apply(X, 2, table)

or the second version (a more robust one, but as inefficient as the first solution):

k <- sort(unique(c(X)))
apply(X, 2, function (u) table(factor(u, levels = k)) )

The new answer above is kind of "overkill" for your example, but is more useful in practice (I think).

Create contingency table that displays the frequency distribution of pairs of variables

You could do

sapply(split(df, df$gender), function(x) colSums(x[names(x)!="gender"]))    

#> 0 1
#> Horror 1 1
#> Thriller 1 3
#> Comedy 0 0
#> Romantic 0 0
#> Sci.fi 1 3


Related Topics



Leave a reply



Submit