R: group-wise min or max

后端 未结 3 494
礼貌的吻别
礼貌的吻别 2020-12-21 13:42

There are so many posts on how to get the group-wise min or max with SQL. But how do you do it in R?

Let\'s say, you have got the following data frame



        
3条回答
  •  孤城傲影
    2020-12-21 14:04

    df is your data.frame -

    library(data.table)
    
    setDT(df) # convert to data.table in place
    
    df[, value[which.min(t)], by = ID]
    

    Output -

    > df[, value[which.min(t)], by = ID]
       ID V1
    1:  a  3
    2:  b  2
    

提交回复
热议问题