与data.table按组分组比较以汇总data.table [英] Subset by group with data.table compared to aggregate a data.table
问题描述
这是对按数据分组的子集的后续问题.表使用相同的数据.表:
This is a follow up question to Subset by group with data.table using the same data.table:
library(data.table)
bdt <- as.data.table(baseball)
# Aggregating and loosing information on other columns
dt1 <- bdt[ , .(max_g = max(g)), by = id]
# Aggregating and keeping information on other columns
dt2 <- bdt[bdt[, .I[g == max(g)], by = id]$V1]
为什么 dt1
和 dt2
的行数不同?dt2是否仅在不丢失其他各列中的相应信息的情况下才具有相同的结果?
Why do dt1
and dt2
differ in number of rows?
Isn't dt2 supposed to have the same result just without loosing the respective information in the other columns?
推荐答案
@Frank指出:
bdt [,.(max_g = max(g)),by = id]
为您提供最大值,而
bdt [bdt [,.I [g == max(g)],由= id] $ V1]
标识具有此最大值的所有行.
bdt[bdt[ , .I[g == max(g)], by = id]$V1]
identifies all rows that have this maximum.
请参见 有什么区别arg max 和 max ?进行数学解释,然后在R中尝试使用此苗条版本:
See What is the difference between arg max and max? for a mathematical explanation and try this slim version in R:
library(data.table)
bdt <- as.data.table(baseball)
dt <- bdt[id == "woodge01"][order(-g)]
dt[ , .(max = max(g)), by = id]
dt[ dt[ , .I[g == max(g)], by = id]$V1 ]
这篇关于与data.table按组分组比较以汇总data.table的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!