如何在RHS上使用变量列名:= operations?例如,给定此data.table"dt",我想创建两个新列"first_y"和"first_z",其中包含对"x"值的给定列的第一次观察.
dt <- data.table(x = c("one","one","two","two","three"), y = c("a", "b", "c", "d", "e"), z = c(1, 2, 3, 4, 5)) dt x y z 1: one a 1 2: one b 2 3: two c 3 4: two d 4 5: three e 5
如果没有变量列名,您可以这样做.
dt[, c("first_y", "first_z") := .(first(y), first(z)), by = x] dt x y z first_y first_z 1: one a 1 a 1 2: one b 2 a 1 3: two c 3 c 3 4: two d 4 c 3 5: three e 5 e 5
但是,如果"y"和"z"列名称动态存储在变量中,我该怎么做呢?
cols <- c("y", "z") # This doesn't work dt[, (paste0("first_", cols)) := .(first(cols)), by = x] # Nor does this q <- quote(first(as.name(cols[1]))) p <- quote(first(as.name(cols[2]))) dt[, (paste0("first_", cols)) := .(eval(q), eval(p)), by = x]
我已经尝试了很多其他的quote()和eval()以及as.name()的组合而没有成功.该操作的LHS似乎按预期工作,并在许多地方记录,但我找不到有关在RHS上使用变量列名称的任何信息.提前致谢.
我不熟悉这个first
功能(虽然它看起来像是Hadley所定义的).
dt[, paste0("first_", cols) := lapply(.SD, head, n = 1L), by = x, .SDcols = cols] # x y z first_y first_z #1: one a 1 a 1 #2: one b 2 a 1 #3: two c 3 c 3 #4: two d 4 c 3 #5: three e 5 e 5