Contingency table when you have a column with count values

Viewed 279

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?

3 Answers

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

In tidyverse with the help of janitor you can do :

library(tidyr)
library(janitor)

df %>%
  pivot_wider(names_from = Var, values_from = Count) %>%
  adorn_totals(where = c("row", "col"))

#  Year  A  B Total
#  2019 10 36    46
#  2020 42 23    65
# Total 52 59   111

A base R option using reshape to make it "wide", i.e.,

reshape(
  df,
  direction = "wide",
  idvar = "Year",
  timevar = "Var"
)

gives

  Year Count.A Count.B
1 2019      10      36
2 2020      42      23
Related