Cross Table of 5 Variables

Good day,
I will appreciate your help. I am a new R user.
Kindly, I have a data set as follow (example):
|Var1|Var2|Var3|Var4|Var5|
|0|0|0|0|0|
|0|0|0|0|1|
|0|0|0|0|2|
|0|0|0|1|0|
Variables values are as follow:
Var1 bear values (0,1,2,3)
Var2, Var3, Var4, Var5 bear values (0,1,2)

I would like to build cross table that contains all possible values with its count such as follow:
|Var1|Var2|Var3|Var4|Var5|count|
|0|0|0|0|0|0|
|0|0|0|0|1|0|
|0|0|0|0|2|0|
|0|0|0|1|0|1|
|0|0|0|1|1|1|
|0|0|0|1|2|0|
|0|0|0|2|0|6|
So |0|0|0|0|0| never appear , |0|0|0|2|0| appears 6 times (6 rows) and so on.

I tried both table, crosstab and xtab but couldn't figure it out.
Regards,
Ohoud

Can you provide so usable sample data? A handy way to supply sample data is to use the dput() function. See ?dput. If you have a very large data set then something like head(dput(myfile), 100) will likely supply enough data for us to work with.

You may also find this useful:

How to produce a
reproducible example (reprex)

if your variables are factors with known levels, then dplyr::group_by* variants can summarise for all levels even non present ones.


set.seed(42)

(exdf <- data.frame(a=factor(sample.int(3,size=3,replace=TRUE)-1,levels=(0:2)),
           b=factor(sample.int(3,size=3,replace=TRUE)-1,levels=(0:2)),
          c=factor(sample.int(3,size=3,replace=TRUE)-1,levels=(0:2))))

library(tidyverse)

group_by_all(exdf,.drop = FALSE) %>% 
  summarise(n=n()) %>% ungroup()

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.