How to emulate SQLs rank functions in R? How to emulate SQLs rank functions in R? sql sql