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

When I looked through the answers here , I found this solution to work 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 started with data.tablethis, it led to something other than my understanding.

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 expected behavior? If so, is there any directive on when not to use the backend data.tablewith dplyr?

+4
source share
1 answer

The error causing the grouping reset after mutateon data.table was resolved at 0.5.0 .

+4
source

Source: https://habr.com/ru/post/1607301/


All Articles