Skip to content

data.table average by group provide different results when using mean() and sum()/.N #5372

Closed

Description

@fabiocs8

This issue is fully documented in https://stackoverflow.com/questions/71983259

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
> packageVersion('data.table')
[1] ‘1.14.3’
> sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=Portuguese_Brazil.1252  LC_CTYPE=Portuguese_Brazil.1252    LC_MONETARY=Portuguese_Brazil.1252
[4] LC_NUMERIC=C                       LC_TIME=Portuguese_Brazil.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] zoo_1.8-10        lubridate_1.8.0   RPostgres_1.4.3   DBI_1.1.2         stringi_1.7.6     readxl_1.4.0     
 [7] gsubfn_0.7        proto_1.0.0       stringr_1.4.0     magrittr_2.0.3    stringdist_0.9.8  fuzzyjoin_0.1.6  
[13] data.table_1.14.3

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.8.3     pillar_1.7.0     compiler_4.1.0   cellranger_1.1.0 tools_4.1.0      bit_4.0.4       
 [7] lattice_0.20-44  lifecycle_1.0.1  tibble_3.1.6     pkgconfig_2.0.3  rlang_1.0.2      cli_3.2.0       
[13] rstudioapi_0.13  writexl_1.4.0    parallel_4.1.0   dplyr_1.0.8      hms_1.1.1        generics_0.1.2  
[19] vctrs_0.4.1      grid_4.1.0       bit64_4.0.5      tidyselect_1.1.2 glue_1.6.2       R6_2.5.1        
[25] fansi_1.0.3      tcltk_4.1.0      blob_1.2.3       purrr_0.3.4      ellipsis_0.3.2   assertthat_0.2.1
[31] utf8_1.2.2       crayon_1.5.1

As suggested by akrun in the SO link above, options(datatable.optimize=1) fixes the problem.

Activity

  1. avimallu commented on Apr 23, 2022

    @avimallu
    Contributor

    I thought similar issues like this were resolved in #5348, but I guess this is still there.

  2. jangorecki commented on Apr 24, 2022

    @jangorecki
    Member

    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()

  3. fabiocs8 commented on Apr 24, 2022

    @fabiocs8
    Author

    data.table:::.git()
    [1] "eed712ef45fd9198de6aa1ac1b672a7347253d18"

  4. fabiocs8 commented on Apr 24, 2022

    @fabiocs8
    Author

    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:

    data.table:::.git()
    [1] "e9a323de01a17af70d5316016606fa8d35b25023"
    Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions