Quick note regarding similar questions: This question was initially flagged as similar to How to emulate SQLs rank functions in R?, and How to get ranks with no gaps when there are ties among values?. However, the first reference assumes the user understands the SQL rank function, which I and probably many other users don't know SQL or its rank function, and the second reference addresses ranking tied elements whereas my question addresses both ties and large gaps (>1) between the elements. I believe my question title is broader and better verbalizes the issue for users. Thus I petition to leave this question stand.
The original question re: relative rankings of elements: I've been playing around with rank()
, order()
, seq()
, list()
, and unlist()
functions in R in order to get the relative ranking of each element in a list in R. In the two examples illustrated below, I'm trying to derive the yellow columns showing the relative rank of each element. How can this be done? I have a preference for dplyr if it's easier to execute than in base R.
When I run the following code for the illustrated Example 1, I get these results which is not what I want:
> rank(Example1$Element)
[1] 3 3 3 6 3 7 3
Example1 <- data.frame(Element = c(1,1,1,2,1,3,1))
rank(Example1$Element)