How to find mode across variables/vectors within a data row in R

后端 未结 3 633
谎友^
谎友^ 2021-01-20 09:55

Does anyone know how to find the mode (most frequent across variables for a single case in R?

For example, if I had data on favorite type of fruit (x), asked nine t

3条回答
  •  孤街浪徒
    2021-01-20 10:22

    A solution that chooses the lowest value for ties is given by:

    modeStat = function(vals) {
      return(as.numeric(names(which.max(table(vals)))))
    }
    modeStat(c(1,3,5,6,4,5))
    

    This returns:

    [1] 5
    

提交回复
热议问题