Search code examples
rtidyversecontingency

Contingency table when you have a column with count values


I have a dataframe that looks something like this:

Year      Var    Count
2019       A     10
2020       B     23
2019       B     36
2020       A     42

How can I make a Year x Var contingency table using the "Count" column as frequencies?


Solution

  • 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))