The following code segfaults my R 2.15.0
, running data.table 1.8.9
.
library(data.table)
d = data.table(date = c(1,2,3,4,5), value = c(1,2,3,4,5))
# works as expected
d[-5][, mean(value), by = list(I(as.integer((date+1)/2)))]
# crashes R
d[-5, mean(value), by = list(I(as.integer((date+1)/2)))]
And on a related note, the following two commands have very different outputs:
d[-5][, value, by = list(I(as.integer((date+1)/2)))]
# I value
# 1: 1 1
# 2: 1 2
# 3: 2 3
# 4: 2 4
d[-5, value, by = list(I(as.integer((date+1)/2)))]
# I value
# 1: 1 2.121996e-314
# 2: 1 2.470328e-323
# 3: 2 3.920509e-316
# 4: 2 2.470328e-323
Simpler command crashing my R
from the comments:
d[-5, value, by = date]
As Ricardo points out, it's the combination of negative indexing and by
that creates the problem.