I occasionally get this error when I'm using data table. I had a hard time coming up with an example to replicate the error, so I apologize that this one is not very realistic.
(numbers vary for N and J)
The error seems to happen most often when I use the unique
function in the j column.
DT = data.table(
group1 = rep(c('a', 'b', 'c', 'd'), each = 3),
group2 = rep(c('w', 'x', 'y', 'z'), times = 3),
values = rep(1:6, times = 2))
## Works:
DT[i=TRUE, j=list(unique(group1), group1, .N), keyby=list(group2)]
## Error:
DT = rbind(DT, DT[1])
DT[i=TRUE, j=list(unique(group1), group1, .N), keyby=list(group2)]
Another similar example is as follows:
set.seed(3)
DT = data.table(
group1 = sample(c('a', 'b', 'c', 'd'), 1000, replace=TRUE),
group2 = sample(c('w', 'x', 'y', 'z'), 1000, replace=TRUE),
values = sample(1:20, replace = TRUE))
DT[, j=list(unique(group1), group1), keyby=list(group2)]
The first example gives numbers that seem to relate to the actual data, but the second example comes up with a strange number.
Error in `[.data.table`(DT, , j = list(unique(group1), group1), keyby = list(group2)) : maxn (242) is not exact multiple of this j column's length (4)
Can someone tell me what causes this?