Search code examples
rlistsortingranking

Ranking based on significant difference in list of lists


I have a list of lists, where each list is sorted. What I want to look into is how many times a certain element has appeared in a specific position and take into account if the value is significantly different from the previous element. (For example in the first list gs, ms, bofa and citi have the same value but they are ranked differently). I realize that the length of the rankings would not be the same because each list's elements are different. How can I do this in a manner where it is correct and also shows the result in a decent way?

What I have so far with ranking solidly based on the sort function output:

dput(degree.l)
list(c(schwab = 0, pnc = 0.0344827586206897, jpm = 0.0862068965517241, 
amex = 0.0862068965517241, gs = 0.103448275862069, ms = 0.103448275862069, 
bofa = 0.103448275862069, citi = 0.103448275862069, wf = 0.120689655172414, 
spgl = 0.120689655172414, brk = 0.137931034482759), c(schwab = 0.0166666666666667, 
pnc = 0.05, ms = 0.0666666666666667, spgl = 0.0833333333333333, 
jpm = 0.1, bofa = 0.1, wf = 0.1, amex = 0.1, gs = 0.116666666666667, 
brk = 0.116666666666667, citi = 0.15), c(schwab = 0.0428571428571429, 
gs = 0.0714285714285714, pnc = 0.0714285714285714, citi = 0.0857142857142857, 
amex = 0.0857142857142857, spgl = 0.0857142857142857, jpm = 0.1, 
brk = 0.1, ms = 0.114285714285714, wf = 0.114285714285714, bofa = 0.128571428571429
))

table(res <- sapply(degree.l, names), pos = row(res))

Also refer to this question for ways to rank it solidly based on the sort I got:

Frequency of appearance in list of lists


Solution

  • We loop over the named list ('degree.l'), get the rank on the rounded vectors with dense_rank, named it with the original vector names, stack into a two column data.frame, rbind the list elements and get the frequency table

    table(do.call(rbind, lapply(degree.l, function(x) 
        stack(setNames(dplyr::dense_rank(round(x, 3)), names(x)))))[2:1])
    

    -output

           values
    ind      1 2 3 4 5 6 7
      schwab 3 0 0 0 0 0 0
      pnc    0 3 0 0 0 0 0
      jpm    0 0 1 1 1 0 0
      amex   0 0 2 0 1 0 0
      gs     0 1 0 1 0 1 0
      ms     0 0 1 1 1 0 0
      bofa   0 0 0 1 1 1 0
      citi   0 0 1 1 0 0 1
      wf     0 0 0 0 3 0 0
      spgl   0 0 1 1 1 0 0
      brk    0 0 0 1 0 2 0