Home > Mobile >  How to get the most frequent level of a categorical variable in R when the variable has two level?
How to get the most frequent level of a categorical variable in R when the variable has two level?

Time:01-18

I have a data set that has variable sex, has two levels, male and female, and another categorical variable which has 6 levels, I want to find the most frequent of the second variable for male and females, I mean which levels of this second variable have the most frequency for males and females,

thank you

CodePudding user response:

Assuming it's ok to infer this from a table, a simple frequency table will do it:

table1 <- table(dataset$sex, dataset$var2)
table1

Obviously substitute in your dataset's name and whatever you've called your second variable. The output will be a frequency table and you can easily read along each row to see the most frequent category for each sex.

  •  Tags:  
  • Related