如何在 R 中对数据 table、分组和计算 j 进行子集化

How to subset data table, group, and compute on j, in R

我正在尝试从数据 table dt 修改列 newCol,仅针对组 g1 和 [=16] 中最小的 year =].

这是一个最小的例子。如您所见,我成功地做了我想做的事,但我想知道是否有更优雅或 'datatablish' 的方式来做这件事。如果可能,我只想以 data.table 格式回答!

library(data.table)
# Dummy data
dt = data.table(year = c(2000, 2001, 2003, 2001, 2005, 2000, 2008),
    g1 = c(1, 1, 1, 2, 2, 3, 3), g2 = c(88, 88, 88, 88, 88, 54, 54))

# Set up new col to foo
dt[, newCol := "foo"]

# Correct the value for the minimal year, by group g1 and g2
dt[dt[, .I[which.min(year)], by = .(g1, g2)][, V1], newCol := "bar"]

我们可以在一行中使用

dt[,   newCol := c("foo", "bar")[1 + (year == min(year))], .(g1, g2)]