对于按站点分组、按时间 t 排序的 data.table DT,我需要更改每个组中变量的最后一个值。我认为应该可以通过引用使用 := 来做到这一点,但我还没有找到一种有效的方法。
样本数据:
require(data.table) # using 1.8.11
DT <- data.table(site=c(rep("A",5), rep("B",4)),t=c(1:5,1:4),a=as.double(c(11:15,21:24)))
setkey(DT, site, t)
DT
# site t a
# 1: A 1 11
# 2: A 2 12
# 3: A 3 13
# 4: A 4 14
# 5: A 5 15
# 6: B 1 21
# 7: B 2 22
# 8: B 3 23
# 9: B 4 24
想要的结果是将每组中 a 的最后一个值更改为 999,因此结果如下所示:
# site t a
# 1: A 1 11
# 2: A 2 12
# 3: A 3 13
# 4: A 4 14
# 5: A 5 999
# 6: B 1 21
# 7: B 2 22
# 8: B 3 23
# 9: B 4 999
似乎应该使用 .I 和/或 .N ,但我还没有找到有效的表格。在与 .I[.N] 相同的语句中使用 := 会产生错误。下面给出了要进行分配的行号:
DT[, .I[.N], by=site]
# site V1
# 1: A 5
# 2: B 9
但我似乎无法将它与 := 赋值一起使用。以下给出错误:
DT[.N, a:=999, by=site]
# Null data.table (0 rows and 0 cols)
DT[, .I[.N, a:=999], by=site]
# Error in `:=`(a, 999) :
# := and `:=`(...) are defined for use in j, once only and in particular ways.
# See help(":="). Check is.data.table(DT) is TRUE.
DT[.I[.N], a:=999, by=site]
# Null data.table (0 rows and 0 cols)
有没有办法通过 data.table 中的引用来做到这一点?或者这在R中以另一种方式做得更好?
最佳答案
目前您可以使用:
DT[DT[, .I[.N], by = site][['V1']], a := 999]
# or, avoiding the overhead of a second call to `[.data.table`
set(DT, i = DT[,.I[.N],by='site'][['V1']], j = 'a', value = 999L)
替代方法:
使用
replace
...DT[, a := replace(a, .N, 999), by = site]
或将替换转移到 RHS,由
{}
包裹并返回完整的向量DT[, a := {a[.N] <- 999L; a}, by = site]
或使用
mult='last'
并利用 by-without-by
.这要求 data.table 由感兴趣的组键入。 DT[unique(site), a := 999, mult = 'last']
有一个功能请求 #2793这将允许
DT[, a[.N] := 999]
但这还有待实现
关于r - 如何通过引用更改每个组中的最后一个值,在 data.table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21819253/