Skip to content

optimized by group gvar() returns error: "negative length vectors are not allowed" when within-group variance is 0 #2111

@osofr

Description

@osofr

Using latest development version of data.table (1.10.5):

This returns an error:

library(data.table)
testDT <- data.table(col1 = c(1,1,1, 2,2,2), col2 = c(2,2,2,1,1,1), ID = c(rep(1,3), rep(2,3)))
setkeyv(testDT, "ID")
testDT[, lapply(.SD, var), by = ID]

Error in gvar(col1) : negative length vectors are not allowed

This however works fine:

testDT[, lapply(.SD, stats::var), by = ID]
   ID col1 col2
1:  1    0    0
2:  2    0    0

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions