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
Divide All Columns by the Value from the 2Nd Column - Apply for All Rows
How to Change the Default Colors in Plotly Chart
Split an Audio File into Pieces of an Arbitrary Size
Change R Default Library Path Using .Libpaths in Rprofile.Site Fails to Work
How to Change Y Axis Limits in Decimal Points in R
Numbering Rows Within Groups in a Data Frame
Add Regression Line Equation and R^2 on Graph
Pass a Data.Frame Column Name to a Function
Select/Assign to Data.Table When Variable Names Are Stored in a Character Vector
Complete Dataframe With Missing Combinations of Values
Selecting Data Frame Rows Based on Partial String Match in a Column
Access Lapply Index Names Inside Fun
Why Does Data.Table Update Names(Dt) by Reference, Even If I Assign to Another Variable
Generate List of All Possible Combinations of Elements of Vector