Operations with "by" do not dispatch to S3 methods

Open
#3,533 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Start with the supplied reprex using circular::circular, plain mean, and by = index, then trace data.table's grouped by evaluation and S3 method dispatch. Done means plain mean(direction) inside by dispatches to circular::mean.circular and returns 5, while the existing non-by cases remain correct.

Written by the indexing model from the issue text.

Description

non-atomic column

When using by, it appears that data.table is not able to dispatch S3 methods. The closest similar issue I could find was with this old SO one which was fixed in v1.9.6: https://stackoverflow.com/questions/33177009/s3-method-dispatch-in-data-table-when-using-by-clause. In this case, however, although it DOES maintain the class of the column being operated on, it does not dispatch the correct S3 method unless that is explicitly provided. In the reprex below, direction is of class circular and mean(c(20, 350)) should give 5, not 185. This works without by or when using by and explicitly calling circular::mean.circular, but not when using by and plain old mean:

library(data.table)

dt <- data.table(
  index = c("A", "A"),
  direction = circular::circular(
    c(20, 350),
    template = 'geographics',
    modulo = '2pi',
    units = 'degrees'
  )
)
dt
#>    index direction
#> 1:     A        20
#> 2:     A       350
dt[, mean(direction)]  # correct
#> Circular Data: 
#> Type = angles 
#> Units = degrees 
#> Template = geographics 
#> Modulo = 2pi 
#> Zero = 1.570796 
#> Rotation = clock 
#> [1] 5
dt[, mean(direction), by = index]$V1 # wrong!
#> Circular Data: 
#> Type = angles 
#> Units = degrees 
#> Template = geographics 
#> Modulo = 2pi 
#> Zero = 1.570796 
#> Rotation = clock 
#> [1] 185
dt[, circular::mean.circular(direction), by = index]$V1 # correct
#> Circular Data: 
#> Type = angles 
#> Units = degrees 
#> Template = geographics 
#> Modulo = 2pi 
#> Zero = 1.570796 
#> Rotation = clock 
#> [1] 5

sessionInfo()
#> R version 3.5.2 (2018-12-20)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 16.04.5 LTS
#> 
#> Matrix products: default
#> BLAS: /usr/lib/libblas/libblas.so.3.6.0
#> LAPACK: /usr/lib/lapack/liblapack.so.3.6.0
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
#>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] data.table_1.12.2
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.1      mvtnorm_1.0-10  digest_0.6.18   magrittr_1.5   
#>  [5] evaluate_0.13   highr_0.8       stringi_1.4.3   boot_1.3-20    
#>  [9] rmarkdown_1.12  tools_3.5.2     stringr_1.4.0   xfun_0.6       
#> [13] yaml_2.2.0      compiler_3.5.2  circular_0.4-93 htmltools_0.3.6
#> [17] knitr_1.22
Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.