Operations with "by" do not dispatch to S3 methods
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 45/100
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
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Rdatatable/data.table
-
as.data.table() recurses without end on a survival::Surv object (or any data.frame carrying one) Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Rdatatable/data.table#7887 ·
-
consistency tests
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Rdatatable/data.table#7853 · 3 comments ·
-
internals
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Rdatatable/data.table#6938 · 1 comment ·
-
encoding fread
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Rdatatable/data.table#5179 · 8 comments ·
-
documentation programming
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Rdatatable/data.table#3199 · 3 comments ·
All issues in Rdatatable/data.table
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
r-lib/pkgdepends#485 · 3 comments ·
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
beginners blocker
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enviPathR OpenBuild Error Build OK Build Warning policies-accepted pre-review precheck-passed
Difficulty 1/5 Under an hour Newbie friendliness 84/100
Bioconductor/BiocContributions#207 · 6 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
datacarpentry/semester-biology#1255 ·