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 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