Aggregate() - R - 是否可以屏蔽聚合列之一以防止显示和添加另一列

Aggregate() - R - Is is possible to mask one of the aggregated columns from displaying and adding another column

我有一个 "emp" 数据集,其中包含名称、等级和值(基于经理的反馈)

  name grade value
1  Ram     R   2.1
2  Sam     R   2.4
3  Jam     R   5.3
4 Bill     S   4.2
5 Claw     S   3.6
6  Men     S   1.2
7  Jay     P   5.3
8  Kay     P   3.8
9  Ray     P   3.2

有了aggregate(value ~ grade, data = emp, FUN=min)我得到了每个等级的最小值

  grade value
1     P   3.2
2     R   2.1
3     S   1.2

基于最小值,我只想显示成绩和姓名而不是值列。 R.

中的 aggregate() 是否可行

这是一种可能的方法

library(data.table)
setDT(emp)[, .(name = name[which.min(value)]), by = grade]
#    grade name
# 1:     R  Ram
# 2:     S  Men
# 3:     P  Ray

这是另一个

library(dplyr)
emp %>%
  group_by(grade) %>%
  summarise(name = name[which.min(value)])

# Source: local data table [3 x 2]
# 
#   grade name
# 1     R  Ram
# 2     S  Men
# 3     P  Ray

或以 R 为基数

do.call(rbind, by(emp, emp$grade, 
                  function(x) data.frame(grade = as.character(x$grade[1L]), 
                                         name = x$name[which.min(x$value)])))
#   grade name
# P     P  Ray
# R     R  Ram
# S     S  Men