下面是一个简单的示例,其中keyby(也按by)不返回带有子集的唯一组。
但是,一旦删除了子设置,keyby便可以正常工作。
library(data.table)
# data.table 1.10.5 IN DEVELOPMENT built 2018-03-21 23:49:00 UTC; travis
# The fastest way to learn (by data.table authors): https://www.datacamp.com/courses/data-analysis-the-data-table-way
# Documentation: ?data.table, example(data.table) and browseVignettes("data.table")
# Release notes, videos and slides: http://r-datatable.com
# small dataset
dat <- data.table(Group = rep(c("All", "Not All"), times = 4), count = 1:8, ID = rep(1:2, each = 4))
# keyby returning non unique IDs with subset
dat[Group == "All" ,lapply(.SD, function(x) sum(x, na.rm = TRUE)), .SDcols= c("count"), keyby = ID, verbose = TRUE]
# Creating new index 'Group'
# Creating index Group done in ... 0.001sec
# Optimized subsetting with index 'Group'
# on= matches existing index, using index
# Starting bmerge ...done in 0.000sec
# i clause present and columns used in by detected, only these subset: ID
# Finding groups using forderv ... 0.000sec
# Finding group sizes from the positions (can be avoided to save RAM) ... 0.000sec
# lapply optimization changed j from 'lapply(.SD, function(x) sum(x, na.rm = TRUE))' to 'list(..FUN1(count))'
# GForce is on, left j unchanged
# Old mean optimization is on, left j unchanged.
# Making each group and running j (GForce FALSE) ...
# collecting discontiguous groups took 0.000s for 2 groups
# eval(j) took 0.000s for 2 calls
# 0.000sec
# ID count
# 1: 1 4
# 2: 1 12
# keyby working fine without subset
dat[,lapply(.SD, function(x) sum(x, na.rm = TRUE)), .SDcols= c("count"), keyby = ID]
# Finding groups using forderv ... 0.000sec
# Finding group sizes from the positions (can be avoided to save RAM) ... 0.000sec
# lapply optimization changed j from 'lapply(.SD, function(x) sum(x, na.rm = TRUE))' to 'list(..FUN1(count))'
# GForce is on, left j unchanged
# Old mean optimization is on, left j unchanged.
# Making each group and running j (GForce FALSE) ...
# memcpy contiguous groups took 0.000s for 2 groups
# eval(j) took 0.000s for 2 calls
# 0.000sec
# ID count
# 1: 1 10
# 2: 2 26
sessionInfo()
R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 9 (stretch)
Matrix products: default
BLAS: /usr/lib/openblas-base/libblas.so.3
LAPACK: /usr/lib/libopenblasp-r0.2.19.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=C
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] data.table_1.10.5
loaded via a namespace (and not attached):
[1] compiler_3.4.4
同意这是一个错误。
作为记录,在这种情况下推荐的代码是:
dat[Group == "All", lapply(.SD, sum, na.rm = TRUE), .SDcols= c("count"), keyby = ID]
这将给出正确的答案,因为此版本将激活GForce
并且在这种情况下不存在该错误。
当然,如果您不能像这样混淆您的实际代码,这是没有帮助的。
有趣的是,如果我们直接传递子集行,则代码将起作用:
dat[c(1, 3, 5, 7),
lapply(.SD, function(x) sum(x, na.rm = TRUE)),
.SDcols= "count", keyby = ID, verbose = TRUE]
# i clause present and columns used in by detected, only these subset: ID
# Finding groups using forderv ... 0.000sec
# Finding group sizes from the positions (can be avoided to save RAM) ... 0.000sec
# lapply optimization changed j from 'lapply(.SD, function(x) sum(x, na.rm = TRUE))' to 'list(..FUN1(count))'
# GForce is on, left j unchanged
# Old mean optimization is on, left j unchanged.
# Making each group and running j (GForce FALSE) ...
# collecting discontiguous groups took 0.000s for 2 groups
# eval(j) took 0.000s for 2 calls
# 0.000sec
# ID count
# 1: 1 4
# 2: 2 12
我在verbose
输出中看到以下差异:
使用“组”索引优化子集
这导致我从CRAN安装; 代码在1.10.4-3
上运行没有错误。
所以我想这是@MarkusBonsch在子集优化方面的
如果我们使连接明确,我也会看到相同的错误:
dat[.('All'), on = 'Group',
lapply(.SD, function(x) sum(x, na.rm = TRUE)),
.SDcols= "count", keyby = ID]
# ID count
# 1: 1 4
# 2: 1 12
但是键控版本很好:
setkey(dat, Group)
dat[.('All'),
lapply(.SD, function(x) sum(x, na.rm = TRUE)),
.SDcols= "count", keyby = ID]# ID count
# 1: 1 4
# 2: 2 12
感谢@cathine的报告和@MichaelChirico的调查。
根本原因是迈克尔指出的联接版本的错误行为:
dat[.('All'), on = 'Group', lapply(.SD, function(x) sum(x, na.rm = TRUE)), .SDcols= "count", keyby = ID]
解决此问题#2591时,可能会解决问题。
在新的子集优化中,子集被重定向到data.table
联接部分,因此,此错误现在影响现在的子集以及联接。 如果我能解决问题,我会尽快调查。
在此之前,您可以诉诸
dat[Group == "All"][ ,lapply(.SD, function(x) sum(x, na.rm = TRUE)), .SDcols= c("count"), keyby = ID, verbose = TRUE]
。
带来不便敬请谅解。
谢谢@cathine! 确认这仅是开发人员专用的,可以使用options(datatable.optimize=2)
缓解,因为问题似乎在3级优化中。 我想知道这是如何通过测试的!
来自另一个也报告过的联系人的更简单的示例:
> DT = data.table(
id = c("a","a","a","b","b","c","c","d","d"),
group = c(1,1,1,1,1,2,2,2,2),
num = 1)
> DT[, uniqueN(id), by=group] # ok
group V1
<num> <int>
1: 1 2
2: 2 2
> DT[num==1, uniqueN(id), by=group] # group column wrong
group V1
<num> <int>
1: 1 2
2: 1 2
> options(datatable.optimize=2)
> DT[num==1, uniqueN(id), by=group] # ok
group V1
<num> <int>
1: 1 2
2: 2 2
> options(datatable.optimize=3) # not ok
> DT[num==1, uniqueN(id), by=group]
group V1
<num> <int>
1: 1 2
2: 1 2
> DT[num==1, sum(num), by=group] # ok
group V1
<num> <num>
1: 1 7
2: 2 4
> DT[num==1, length(num), by=group] # not ok
group V1
<num> <int>
1: 1 7
2: 1 4
> options(datatable.optimize=2) # ok
> DT[num==1, length(num), by=group]
group V1
<num> <int>
1: 1 7
2: 2 4
>
为什么它会通过测试? 因为仅在对分组列进行排序时才会发生(请参见下面的代码)! 我没有特别检查已排序列的分组。
library(data.table)
DT = data.table(
id = c("a","a","a","b","b","c","c","d","d"),
group = c(1,1,1,1,1,2,2,2,2),
group2 = c(1,1,1,1,1,2,2,2,1),
num = 1)
DT[, uniqueN(id), by=group] # ok
# group V1
# <num> <int>
# 1: 1 2
# 2: 2 2
DT[num==1, uniqueN(id), by=group] # group column wrong
# group V1
# <num> <int>
# 1: 1 2
# 2: 1 2
DT[num==1, uniqueN(id), by=group2] # ok with other group column that is not sorted
# group2 V1
# 1: 1 3
# 2: 2 2
setkey(DT, group2)
DT[num==1, uniqueN(id), by=group2] # not ok anymore since the group column is sorted now
# group2 V1
# 1: 1 3
# 2: 1 2
最有用的评论
同意这是一个错误。
作为记录,在这种情况下推荐的代码是:
这将给出正确的答案,因为此版本将激活
GForce
并且在这种情况下不存在该错误。当然,如果您不能像这样混淆您的实际代码,这是没有帮助的。
有趣的是,如果我们直接传递子集行,则代码将起作用:
我在
verbose
输出中看到以下差异:这导致我从CRAN安装; 代码在
1.10.4-3
上运行没有错误。所以我想这是@MarkusBonsch在子集优化方面的
如果我们使连接明确,我也会看到相同的错误:
但是键控版本很好: