How to select the row with the maximum value in each group

后端 未结 16 2023
北荒
北荒 2020-11-21 04:18

In a dataset with multiple observations for each subject I want to take a subset with only the maximum data value for each record. For example, with a following dataset:

16条回答
  •  青春惊慌失措
    2020-11-21 04:50

    Here's another data.table solution, since which.max does not work on characters

    library(data.table)
    group <- data.table(Subject=ID, pt=Value, Event=Event)
    
    group[, .SD[order(pt, decreasing = TRUE) == 1], by = Subject]
    

提交回复
热议问题