Skip to Content

How to create contigency tables (crosstabs) in R for a subset of columns with categorical data?

I have a table whose header looks like this (I've simplified it):

id, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10

where each row, except for id, is a categorical variable. Let's name the categories A, B, C, D, E.

I would like to create a contingency table for some of the columns, such as below (for brevity, I have not put sample numbers in the cells). Getting the total column/row would be great, but not mandatory, I can calculate it myself later.

      a1  a2  a3  a4 Total

Thus, the question is how to create a crosstab based on multiple columns in R? The examples I've seen with table() and xtabs() use a column only. In my case, the columns are adjacent, so one crosstab would summarize columns a1..a4, another a5..a7 and so on. I hope there is an elegant way to do this.

I'm a programmer, but a newbie in R.

Thank you in advance.