ExperimenteR ExperimenteR - 3 months ago 18
R Question

dplyr bug with data.table backend [in dplyr 0.4.3 or earlier]

As I was browsing through the answers here, I found this solution works exactly as expected with

data.frame
.

library(dplyr) # dplyr_0.4.3
library(data.table) # data.table_1.9.5
df <- structure(list(id = c(1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 3L),
a = c("AA",
"AB", "AA", "AB", "AB", "AB", "AB", "AA", "AA"), b = c(2L, 5L,
1L, 2L, 4L, 4L, 3L, 1L, 4L)), .Names = c("id", "a", "b"),
class = "data.frame", row.names = c(NA, -9L))


df %>%
group_by(id) %>%
mutate(relevance=+(a!='AA')) %>%
mutate(mean=cumsum(relevance * b) / cumsum(relevance))

Source: local data frame [9 x 5]
Groups: id [3]

id a b relevance mean
(int) (chr) (int) (int) (dbl)
1 1 AA 2 0 NaN
2 1 AB 5 1 5.0
3 1 AA 1 0 5.0
4 2 AB 2 1 2.0
5 2 AB 4 1 3.0
6 3 AB 4 1 4.0
7 3 AB 3 1 3.5
8 3 AA 1 0 3.5
9 3 AA 4 0 3.5


However when run with
data.table
, it resulted in something beyond my comprehension.

setDT(df) %>%
group_by(id) %>%
mutate(relevance=+(a!='AA')) %>%
mutate(mean=cumsum(relevance * b) / cumsum(relevance))

Source: local data table [9 x 5]

id a b relevance mean
(int) (chr) (int) (int) (dbl)
1 1 AA 2 0 NaN
2 1 AB 5 1 5.000000
3 1 AA 1 0 5.000000
4 2 AB 2 1 3.500000
5 2 AB 4 1 3.666667
6 3 AB 4 1 3.750000
7 3 AB 3 1 3.600000
8 3 AA 1 0 3.600000
9 3 AA 4 0 3.600000


Is this an expected behaviour? If so, is there any guideline on when not to use
data.table
backend with
dplyr
?

Answer

The bug that causes grouping to be dropped after mutate on a data.table was resolved in 0.5.0.