简体   繁体   中英

How to select max time stamp for each group?

How to select max timestamp for each group in R.

     df <- read.table(text = " ID   obj MR_time
        1599    1   20:05:22
        1599    1   20:06:38
        1599    1   20:07:22
        1599    2   20:08:38
        1599    2   20:09:28", header = TRUE)

dt <- data.table(df)

I required output to be

1599    1   20:07:22
1599    2   20:09:28

我们将其转换为DateTime ,然后按组(假设组为ID,obj)获取索引,以将.SD.SD子集)作为子集

dt[, .SD[which.max(as.POSIXct(MR_time, format = "%H:%M:%S"))], by = .(ID, obj)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM