converts a contingency table or a data frame of counts into a data frame of individual observations.
counts_to_cases(x, count.col = "Freq")
a contingency table or a data frame
the name of the column containing the counts. Default is "Freq".
a data frame of cases
# Create a cross-tabulation demo data
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
xtab <- as.table(
rbind(c(20, 5), c(16,9))
)
dimnames(xtab) <- list(
before = c("non.smoker", "smoker"),
after = c("non.smoker", "smoker")
)
xtab
#> after
#> before non.smoker smoker
#> non.smoker 20 5
#> smoker 16 9
# Convert into a data frame of cases
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
df <- counts_to_cases(xtab)
head(df)
#> before after
#> 1 non.smoker non.smoker
#> 2 non.smoker non.smoker
#> 3 non.smoker non.smoker
#> 4 non.smoker non.smoker
#> 5 non.smoker non.smoker
#> 6 non.smoker non.smoker