Search code examples
rfrequencyranking

Frequency count of 5 rankings in R


Say I have 5 items A, B, C, D, E in a questionnaire and got respondents to rank them. The data looks like this,

> df
  rank1 rank2 rank3 rank4 rank5
1     A     B     C     D     E
2     A     C     B     D     E
3     C     A     B     E     D
4     B     A     C     D     E
5     A     B     D     C     E

How do I count the frequency of each rank by item so the output looks like this,

  item rank1 rank2 rank3 rank4 rank5
1    A     3     2     0     0     0
2    B     1     2     2     0     0
3    C     1     1     2     1     0
4    D     0     0     1     3     1
5    E     0     0     0     1     4

Solution

  • We can use table after converting to factor using base R

    lvls <- sort(unique(unlist(df)))
    sapply(df, function(x) table(factor(x, levels =lvls)))
    #   rank1 rank2 rank3 rank4 rank5
    #A     3     2     0     0     0
    #B     1     2     2     0     0
    #C     1     1     2     1     0
    #D     0     0     1     3     1
    #E     0     0     0     1     4
    

    Or calling table only once

    table(unlist(df), c(col(df)))
    #    1 2 3 4 5
    #  A 3 2 0 0 0
    #  B 1 2 2 0 0
    #  C 1 1 2 1 0
    #  D 0 0 1 3 1
    #  E 0 0 0 1 4
    

    Or compactly with mtabulate from qdapTools

    library(qdapTools)
    t(mtabulate(df))
    

    data

    df <- structure(list(rank1 = c("A", "A", "C", "B", "A"), rank2 = c("B", 
    "C", "A", "A", "B"), rank3 = c("C", "B", "B", "C", "D"), rank4 = c("D", 
    "D", "E", "D", "C"), rank5 = c("E", "E", "D", "E", "E")), .Names = c("rank1", 
     "rank2", "rank3", "rank4", "rank5"), class = "data.frame", row.names = c("1", 
     "2", "3", "4", "5"))