Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 212094

Counts of rows in data table based on values

$
0
0

I want to rank each hour value within each individual id. The hour values that equal each other will be the same rank. I attempted to do this with the row count function (.N) in data.table. I was hoping this would work but I cannot figure it out.

Here is a reproducible example, please let me know if you have any questions about my problem.

library(data.table)

dt <- data.table(hours=c(100, 72, 48, 98, 87, 75, 98, 75, 52, 48, 100, 98, 87, 35, 48, 75, 92, 100, 75, 48),
                 id=rep(1:4, each=5, 1))

dt <- dt[, list(.(hours <= hours), .N), list(hours, id)]

Desired output

counts of rank

Any help will be greatly appreciated.


Viewing all articles
Browse latest Browse all 212094

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>