How to get the most frequent level of a categorical variable in R

前端 未结 3 650
花落未央
花落未央 2020-12-12 06:27

I can get the levels and frequencies of a categorical variable using table() function. But I need to feed the most frequent level into calculations later. How c

3条回答
  •  眼角桃花
    2020-12-12 06:52

    ll<-data.frame(table(a))
    ll[which.max(ll$Freq),]
    

    Example from mtcars data:

    ll<-data.frame(table(mtcars$cyl))
     ll
      Var1 Freq
    1    4   11
    2    6    7
    3    8   14
    
    ll[which.max(ll$Freq),]
      Var1 Freq
    3    8   14
    

提交回复
热议问题