You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
data.table average by group provide different results when using mean() and sum()/.N #5372
I would expect that avg_mean and avg_sum_N would be exactly the same, such as avg_sum_N. Why are they different? Thank you.
qty <- c(1:6)
name <- c("a", "b","a", "a", "c","b" )
type <- c("i", "i", "i", "f", "f", "f")
DT <- data.table(qty,name,type)
DT[, avg_mean := mean(qty) , by = .(name, type)]
DT[, avg_sum_N := sum(qty)/.N , by = .(name, type)]
> DT
qty name type avg_mean avg_sum_N
<int> <char> <char> <num> <num>
1: 1 a i 2 2
2: 2 b i 4 2
3: 3 a i 2 2
4: 4 a f 2 4
5: 5 c f 6 5
6: 6 b f 5 6
As explained, there was a bug in devel version which has been fixed already. Please provide git commit sha of your data.table so we can check if you are on the version that is affected or not. Use data.table:::.git()
Activity
avimallu commentedon Apr 23, 2022
I thought similar issues like this were resolved in #5348, but I guess this is still there.
jangorecki commentedon Apr 24, 2022
As explained, there was a bug in devel version which has been fixed already. Please provide git commit sha of your data.table so we can check if you are on the version that is affected or not. Use
data.table:::.git()fabiocs8 commentedon Apr 24, 2022
fabiocs8 commentedon Apr 24, 2022
I updated the dev data.table through data.table::update.dev.pkg()
The new SHA follows below, it seems that the problem has been fixed: