data.table-通过多个组列选择行

时间:2018-11-27 18:17:24

标签: r data.table subset

一些数据(取自https://www.r-bloggers.com/two-of-my-favorite-data-table-features/

# generate a small dataset
set.seed(1234)
smalldat <- data.frame(group1 = rep(1:2, each = 5), 
                       group2 = rep(c('a','b'), times = 5), 
                       x = rnorm(10))

# convert to data.frame to data.table
library(data.table)
smalldat <- data.table(smalldat)

# convert aggregated variable into raw data file
smalldat[, aggGroup1 := mean(x), by = group1]

# aggregate with 2 variables
smalldat[, aggGroup1.2 := mean(x), by = list(group1, group2)]



Output

##     group1 group2       x aggGroup1 aggGroup1.2
##  1:      1      a -1.2071   -0.3524      0.1022
##  2:      1      b  0.2774   -0.3524     -1.0341
##  3:      1      a  1.0844   -0.3524      0.1022
##  4:      1      b -2.3457   -0.3524     -1.0341
##  5:      1      a  0.4291   -0.3524      0.1022
##  6:      2      b  0.5061   -0.4140     -0.3102
##  7:      2      a -0.5747   -0.4140     -0.5696
##  8:      2      b -0.5466   -0.4140     -0.3102
##  9:      2      a -0.5645   -0.4140     -0.5696
## 10:      2      b -0.8900   -0.4140     -0.3102

如何通过保留aggGroup1.2的信息来选择min具有group1的{​​{1}}值的行。

结果应如下所示:

group2

我尝试使用data.table语法执行此操作,但是失败了...

2 个答案:

答案 0 :(得分:2)

这是一种方法:

smalldat[, .(group2 = group2[which.min(aggGroup1.2)], aggGroup1.2 = min(aggGroup1.2)), by = group1]
#    group1 group2 aggGroup1.2
# 1:      1      b   -1.034134
# 2:      2      a   -0.569596

答案 1 :(得分:2)

除了格里戈尔的答案,还可以。还尝试获取整行:

smalldat[smalldat[, .I[which.min(aggGroup1.2)], by = group1][, V1]]

   group1 group2          x  aggGroup1 aggGroup1.2
1:      1      b  0.2774292 -0.3523537   -1.034134
2:      2      a -0.5747400 -0.4139612   -0.569596