0

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

Samin Ba
  • 13
  • 5
  • Does this answer your question? [How to sum a variable by group](https://stackoverflow.com/questions/1660124/how-to-sum-a-variable-by-group) – NelsonGon Jan 16 '22 at 11:34
  • actually, i found a table which is coded below, now i need to show which category of the second variable has the most frequency, – Samin Ba Jan 17 '22 at 13:08

1 Answers1

0

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.

  • thank you for the answer, i have this table and I need to know is there any code to show exactly the most frequent category for each level of sex variable? – Samin Ba Jan 17 '22 at 13:07
  • I would refer you to this answer for calculating the mode in R. It’s not a standard function so you need to define it yourself: https://stackoverflow.com/a/8189441/14281512 – William Wormell Jan 18 '22 at 12:04