How to get the maximum value by group

前端 未结 5 1084
太阳男子
太阳男子 2020-11-27 07:00

I have a data.frame with two columns: year and score. The years go from 2000-2012 and each year can be listed multiple times. In the s

5条回答
  •  眼角桃花
    2020-11-27 07:31

    data <- data.frame(year = c(2000, 2001, 2000), score = c(18, 22, 21))
    new.year <- unique(data$year)
    new.score <- sapply(new.year, function(y) max(data[data$year == y, ]$score))
    data <- data.frame(year = new.year, score = new.score)
    

提交回复
热议问题