R: group-wise min or max

后端 未结 3 481
礼貌的吻别
礼貌的吻别 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:00

    Two more solutions (with sgibb's df):

    sapply(split(df, df$ID), function(x) x$value[which.min(x$t)])
    
    #a  b  
    #3  2 
    
    library(plyr)
    ddply(df, .(ID), function(x) x$value[which.min(x$t)])
    
    #  ID V1
    #1 a   3
    #2 b   2
    
    0 讨论(0)
  • 2020-12-21 14:01

    You are looking for tapply:

    df <- read.table(textConnection("
    ID | t | value
    a | 1 | 3
    a | 2 | 5
    a | 3 | 2
    a | 4 | 1
    a | 5 | 5
    b | 2 | 2
    b | 3 | 1
    b | 4 | 5"), header=TRUE, sep="|")
    
    m <- tapply(1:nrow(df), df$ID, function(i) {
      df$value[i[which.min(df$t[i])]]
    })
    # a  b
    #  3  2
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题