按组提取对应于变量最小值的行
State
Employees
> data State Company Employees1 AK A 822 AK B 1043 AK C 374 AK D 245 RI E 196 RI F 1187 RI G 888 RI H 42data <- structure(list(State = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("AK", "RI"), class = "factor"), Company = structure(1:8, .Label = c("A", "B", "C", "D", "E", "F", "G", "H"), class = "factor"), Employees = c(82L, 104L, 37L, 24L, 19L, 118L, 88L, 42L)), .Names = c("State", "Company", "Employees"), class = "data.frame", row.names = c(NA, -8L))
min
aggregate
:
> aggregate(Employees ~ State, data, function(x) min(x)) State Employees1 AK 242 RI 19
data.table
:
> library(data.table)> DT <- data.table(data)> DT[ , list(Employees = min(Employees)), by = State] State Employees1: AK 242: RI 19
min
Company