Some time ago, it was discussed whether this is lazy or not. In any case, it is short and natural to write and read (and quickly for big data, so you do not need to change or optimize it later):
require(data.table) DT=as.data.table(airquality) DT[,.SD[which.max(Temp)],by=Month] Month Ozone Solar.R Wind Temp Day [1,] 5 45 252 14.9 81 29 [2,] 6 NA 259 10.9 93 11 [3,] 7 97 267 6.3 92 8 [4,] 8 76 203 9.7 97 28 [5,] 9 73 183 2.8 93 3
.SD
is a subset of the data for each group, and you just want the line to be with it with the highest Temp, iiuc. If you need a line number, then it can be added.
Or get all the lines in which max is bound:
DT[,.SD[Temp==max(Temp)],by=Month] Month Ozone Solar.R Wind Temp Day [1,] 5 45 252 14.9 81 29 [2,] 6 NA 259 10.9 93 11 [3,] 7 97 267 6.3 92 8 [4,] 7 97 272 5.7 92 9 [5,] 8 76 203 9.7 97 28 [6,] 9 73 183 2.8 93 3 [7,] 9 91 189 4.6 93 4
source share