Subsetting with Date produces error

Open
#4,651 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

Start at the [.data.table entry point shown in the backtrace and reproduce the supplied Date-key example. Compare it with test[.(as.Date("2020-01-02"))]; done means direct Date subsetting no longer raises the shown error and returns the matching keyed row.

Written by the indexing model from the issue text.

Description

joins

It is known that using a numeric vector in i will be treated as subsetting by index. However, if the supplied numeric vector has some classes (e.g. Date), then it might make more sense not to subset by index but join (just like a character vector).

Consider the following example:

library(data.table)
test <- data.table(
  date = seq.Date(as.Date("2020-01-01"), as.Date("2020-07-01"), by = "day"),
  key = "date"
)
test[, x := rnorm(.N)]
test[as.Date("2020-01-02")]

The following error is produced:

> test[as.Date("2020-01-02")]                                                                   
Error in `[.data.table`(test, as.Date("2020-01-02")) : 
  i has evaluated to type double. Expecting logical, integer or double.
Backtrace:
1: stop("i has evaluated to type ", typeof(i), ". Expecting logical, integer or double.")
2: `[.data.table`(test, as.Date("2020-01-02"))
3: test[as.Date("2020-01-02")]

User might expect it to behave like

> test[.(as.Date("2020-01-02"))]                                                                
Key: <date>
         date          x
       <Date>      <num>
1: 2020-01-02 -0.2380267
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.