数据表中基于值的行数

问题描述 投票:0回答:1

我想对每个小时ID中的每个小时值进行排名。彼此相等的小时值将是相同的等级。我试图用data.table中的行计数函数(.N)来做到这一点。我希望这能奏效,但我无法弄清楚。

这是一个可复制的示例,如果您对我的问题有任何疑问,请让我知道。

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

所需的输出

counts of rank

任何帮助将不胜感激。

r data.table aggregate rank
1个回答
0
投票

您可以将它们转换为factor,然后转换为integer

library(data.table)
dt[, rank.total := as.integer(factor(hours)), id]
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.