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

后端 未结 16 1931
北荒
北荒 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:47

    Here's a data.table solution:

    require(data.table) ## 1.9.2
    group <- as.data.table(group)
    

    If you want to keep all the entries corresponding to max values of pt within each group:

    group[group[, .I[pt == max(pt)], by=Subject]$V1]
    #    Subject pt Event
    # 1:       1  5     2
    # 2:       2 17     2
    # 3:       3  5     2
    

    If you'd like just the first max value of pt:

    group[group[, .I[which.max(pt)], by=Subject]$V1]
    #    Subject pt Event
    # 1:       1  5     2
    # 2:       2 17     2
    # 3:       3  5     2
    

    In this case, it doesn't make a difference, as there aren't multiple maximum values within any group in your data.

提交回复
热议问题