Rolling join does not retrieve duplicated date in x
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Reproduce the rolling and non-rolling join examples in the issue with duplicated dates and different row ordering. Read the rolling-join behavior in the main help page and determine whether the accepted resolution is a behavior change, documentation, or both. Done means the duplicate-key and ordering behavior is intentional, tested, and clearly documented.
Written by the indexing model from the issue text.
Description
Rolling joins take into account only last occurrence of a duplicated key in x. I think this is both unexpected to the average user and inconsistent with the non-rolling join (aka indexing) which does respect duplicates. The worst part is that currently rolling joins depend on the sorting of the data.
For example
(tx <- data.table(ticker = c("BB", "AA"),
date = as.Date(c("1987-05-29", "1987-05-29"))))
## ticker date
## 1: BB 1987-05-29
## 2: AA 1987-05-29
(ty <- data.table(date = as.Date("1987-05-31")))
## date
## 1: 1987-05-31
tx[ty, on = "date", roll = T]
## ticker date
## 1: AA 1987-05-31
## Alternative sorting results in different join!!
setkey(tx, ticker)
tx[ty, on = "date", roll = T]
## ticker date
## 1: BB 1987-05-31
## Non rolling join (subsetting) does pick the duplicates
tz <- data.table(date = as.Date("1987-05-29"))
tx[tz, on = "date"]
## ticker date
## 1: AA 1987-05-29
## 2: BB 1987-05-29
Because of this subtle issue data corruption can easily go unnoticed with large data. In so many years of using data.table this behavior was never apparent to me and it took me a while to track it down when I noticed it. So at the very least I think the missing match and order dependence must be clearly noted in the main help page.
The simplest workaround I could think of is to create a separate cross-join first and then merge on both ticker and date:
cj <- CJ(ticker = unique(tx$ticker), date = unique(ty$date))
tx[cj, on = c("ticker", "date")]
## ticker date
## 1: AA 1987-05-31
## 2: BB 1987-05-31
EDIT: The above workaround works for this simple case, but for general case where ty is multi-column I don't see a straightforward solution.
devtools::session_info()
─ Session info ─────────────────────────────────────────────────────────────────────────────────────────────────────
setting value
version R Under development (unstable) (2018-12-01 r75733)
os Ubuntu 18.04.1 LTS
system x86_64, linux-gnu
ui X11
language
collate en_GB.UTF-8
ctype en_GB.UTF-8
tz Europe/Amsterdam
date 2018-12-04
─ Packages ─────────────────────────────────────────────────────────────────────────────────────────────────────────
package * version date lib source
assertthat 0.2.0 2017-04-11 [1] CRAN (R 3.5.0)
backports 1.1.2 2017-12-13 [1] CRAN (R 3.5.0)
callr 3.0.0.9002 2018-12-03 [1] Github (r-lib/callr@1cbce9d)
cli 1.0.1 2018-09-25 [1] CRAN (R 3.6.0)
crayon 1.3.4 2017-09-16 [1] CRAN (R 3.5.0)
data.table * 1.11.9 2018-12-03 [1] Github (Rdatatable/data.table@e6913a4)
desc 1.2.0 2018-05-01 [1] CRAN (R 3.6.0)
devtools 2.0.1.9000 2018-12-03 [1] Github (r-lib/devtools@a6bbe87)
digest 0.6.18 2018-10-10 [1] CRAN (R 3.6.0)
fs 1.2.6 2018-08-23 [1] CRAN (R 3.6.0)
glue 1.3.0 2018-07-17 [1] CRAN (R 3.6.0)
magrittr 1.5 2014-11-22 [1] CRAN (R 3.5.0)
memoise 1.1.0 2017-04-21 [1] CRAN (R 3.5.0)
pkgbuild 1.0.2.9000 2018-12-03 [1] Github (r-lib/pkgbuild@6e4ebdf)
pkgload 1.0.1.9000 2018-12-03 [1] Github (r-lib/pkgload@0ef4f58)
prettyunits 1.0.2 2015-07-13 [1] CRAN (R 3.5.0)
processx 3.2.0.9001 2018-12-03 [1] Github (r-lib/processx@366475b)
ps 1.2.1 2018-11-06 [1] CRAN (R 3.6.0)
R6 2.3.0 2018-10-04 [1] CRAN (R 3.6.0)
Rcpp 1.0.0 2018-11-07 [1] CRAN (R 3.6.0)
remotes 2.0.2.9000 2018-12-03 [1] Github (r-lib/remotes@2e3bf35)
rlang 0.3.0.9000 2018-12-03 [1] Github (r-lib/rlang@035d279)
rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.5.0)
sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.6.0)
testthat 2.0.0 2017-12-13 [1] CRAN (R 3.5.0)
usethis 1.4.0 2018-08-14 [1] CRAN (R 3.6.0)
withr 2.1.2.9000 2018-10-23 [1] Github (jimhester/withr@be57595)
[1] /home/vspinu/.lib/3.6.0
[2] /home/vspinu/bin/R-latest/library
>
- 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 ·