Majority vote in R

前端 未结 4 1866
名媛妹妹
名媛妹妹 2021-01-13 02:40

I need to calculate the majority vote for an item in R and I don\'t have a clue how to approach this.

I have a data frame with items and assigned categories. What I

4条回答
  •  萌比男神i
    2021-01-13 03:17

     tdat <- tapply(dat$category, dat$item, function(vec) sort(table(vec), 
                                                     decreasing=TRUE)[1] )
     data.frame(item=rownames(tdat), plurality_vote=tdat)
    
      item plurality_vote
    1    1              3
    2    2              2
    

    A more complex function would be needed to distinguish a plurality (possibly with ties) from a true majority.

提交回复
热议问题