select maximum row value by group

前端 未结 5 2102
渐次进展
渐次进展 2021-01-20 07:45

I\'ve been trying to do this with my data by looking at other posts, but I keep getting an error. My data new looks like this:

id  year    name          


        
5条回答
  •  终归单人心
    2021-01-20 08:01

    Another option that scales well for large tables is using data.table.

    DT <- read.table(text = "id  year    name    gdp
                              1   1980    Jamie   45
                              1   1981    Jamie   60
                              1   1982    Jamie   70
                              2   1990    Kate    40
                              2   1991    Kate    25
                              2   1992    Kate    67
                              3   1994    Joe     35
                              3   1995    Joe     78
                              3   1996    Joe     90",
                     header = TRUE)
    
    require("data.table")
    DT <- as.data.table(DT)
    
    setkey(DT,id,year)
    res = DT[,j=list(year=year[which.max(gdp)]),by=id]
    res
    
    setkey(res,id,year)
    DT[res]
    # id year  name gdp
    # 1:  1 1982 Jamie  70
    # 2:  2 1992  Kate  67
    # 3:  3 1996   Joe  90
    

提交回复
热议问题