Allow a character string in `which` argument of `data.table:::[.data.table`

Open
#6,496 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
38/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Start at the data.table:::[.data.table entry point, focusing on how the existing which values and negated joins are handled. Compare the proposed xmatch, xnomatch, imatch, and inomatch behaviors with the examples in the issue. Done means character-string values return the specified x or i row numbers while preserving existing behavior.

Written by the indexing model from the issue text.

Description

feature request

This is a feature request.

I find which argument quite confusing/counterintuitive when joining and returning i row numbers in x[i, which=NA, ...].

A join and which argument can interact in four different ways as shown below:

x = data.table(a=1:3, x=c(NA, 10, NA))
i = data.table(a=2:5, y=c(20, 10, 20, 30))

x
       a     x
   <int> <num>
1:     1    NA
2:     2    10
3:     3    NA

i
       a     y
   <int> <num>
1:     2    20
2:     3    10
3:     4    20
4:     5    30

x[i, on="a", which=TRUE]     # (a): ok
[1]  2  3 NA NA
x[!i, on="a", which=TRUE]    # (b): ok
[1] 1
x[i, on="a", which=NA]       # (c): counterintuitive
[1] 3 4
x[!i, on="a", which=NA]      # (d): counterintuitive
[1] 1 2

(a): row numbers of x that i matches to.
(b): row numbers of x that no i matches to.
(c): row numbers of i that have no match to x. The fact that i is not prefixed with ! makes it counterintuive.
(d): row numbers of i that have a match to x. The use of ! suggests that the cases that have no match are of interest while it is actually the opposite.

I propose to allow a character string in which with four possible values (other propositions are very welcome): c("xmatch", "xnomatch", "imatch", "inomatch") where they correspond to (a), (b), (d), and (c) scenarios, respectively. These values would work as follow:

x[i, on="a", which="xmatch"]     # row number of x that i matches to
x[i, on="a", which="xnomatch"]   # row numbers of x that no i matches to
x[i, on="a", which="imatch"]     # row numbers of i that have a match to x
x[i, on="a", which="inomatch"]   # row numbers of i that have no match to x

So, the character string specified would allow to know the type of join (whether i needs to be prefixed with ! or not) and the data.table whose row numbers should be returned.

With this feature, data.table:::[.data.table would behave as below:

fm = function(x, i, on, which){
  switch(which,
	 xmatch = x[i, on=on, which=TRUE],
	 xnomatch = x[!i, on=on, which=TRUE],
	 inomatch = x[i, on=on, which=NA],
	 imatch = x[!i, on=on, which=NA])
}

fm(A, B, on="a", which="xmatch")
[1]  2  3 NA NA
fm(A, B, on="a", which="xnomatch")
[1] 1
fm(A, B, on="a", which="imatch")
[1] 1 2
fm(A, B, on="a", which="inomatch")
[1] 3 4
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.