select maximum row value by group

前端 未结 5 2092
渐次进展
渐次进展 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:08

    Just use split:

    df <- do.call(rbind, lapply(split(df, df$id),
      function(subdf) subdf[which.max(subdf$year)[1], ]))
    

    For example,

    df <- data.frame(id = rep(1:10, each = 3), year = round(runif(30,0,10)) + 1980, gdp = round(runif(30, 40, 70)))
    print(head(df))
    #   id year gdp
    # 1  1 1990  49
    # 2  1 1981  47
    # 3  1 1987  69
    # 4  2 1985  57
    # 5  2 1989  41
    # 6  2 1988  54
    
    df <- do.call(rbind, lapply(split(df, df$id), function(subdf) subdf[which.max(subdf$year)[1], ]))
    print(head(df))
    #    id year gdp
    # 1   1 1990  49
    # 2   2 1989  41
    # 3   3 1989  55
    # 4   4 1988  62
    # 5   5 1989  48
    # 6   6 1990  41
    

提交回复
热议问题