Data.table: add fsetdiff(), fintersect(), funion() supports for vectors

Created on 9 Aug 2019  路  3Comments  路  Source: Rdatatable/data.table

Hi,

data.table has already implemented fantastic fsetdiff(), fintersect(), funion() for data.table objects. I would like to proposal their supports on vectors as well. The reason is the equivalent functions in base R will stripe off all the attributes and is bad for Date (or any other inputs with attributes).

vec_date1 <- as.Date(c('1999-01-01', '1999-01-02'))
vec_date2 <- as.Date(c('1999-01-02', '1999-01-03'))
intersect(vec_date1, vec_date2)
#> [1] 10593
setdiff(vec_date1, vec_date2)
#> [1] 10592
union(vec_date1, vec_date2)
#> [1] 10592 10593 10594

Created on 2019-08-09 by the reprex package (v0.2.1)

feature request

Most helpful comment

The first reason is the above solution is too verbose, so verbose that I actually prefer using structure(base::intersect(x, y), class = 'Date').

The second reason is that it will be more consistent with uniqueN(), which supports both data.table and vectors.

I just think setdiff(), intersect() and union() are so commonly used and would be grateful to see data.table() supports a better version than in the base R...

All 3 comments

Any reason why we cannot stick to extra as.data.table call on fintersect input? we could do that for atomic vector internally of course, but I don't see strong arguments for that.

setDT might be more lightweight in terms of performance than as.data.table?

> vintersect = function(x,y) fintersect(setDT(list(x)), setDT(list(y)))$V1
> vintersect(vec_date1, vec_date2)
[1] "1999-01-02"

I guess the argument for it is user convenience.

The first reason is the above solution is too verbose, so verbose that I actually prefer using structure(base::intersect(x, y), class = 'Date').

The second reason is that it will be more consistent with uniqueN(), which supports both data.table and vectors.

I just think setdiff(), intersect() and union() are so commonly used and would be grateful to see data.table() supports a better version than in the base R...

Was this page helpful?
0 / 5 - 0 ratings