按组提取对应于变量最小值的行我希望(1)用一个变量(State),(2)在每个组中找到另一个变量的最小值行(Employees),以及(3)提取整行。(1)和(2)是简单的一行,我觉得(3)也应该是,但我不能理解。下面是一个示例数据集:> 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结果呢?
3 回答
- 3 回答
- 0 关注
- 600 浏览
添加回答
举报
0/150
提交
取消