在给定的数据表中,如何选择V3分组的V2 = max(V2)的列V1的语法。
例如: 在mtcars数据集中,我想找出与max(disp)相等的观测值相对应的hp,按cyl分组。
以下是我的不优雅的解决方案,使用which:
有没有更多“data.table”方式实现相同的结果?
例如: 在mtcars数据集中,我想找出与max(disp)相等的观测值相对应的hp,按cyl分组。
以下是我的不优雅的解决方案,使用which:
mtcars <- data.table(mtcars)
mtcars[which(mtcars$disp %in% mtcars[, max(disp), by = .(cyl)]$V1), .(cyl,hp)]
cyl hp
1: 6 110
2: 4 62
3: 8 205
有没有更多“data.table”方式实现相同的结果?