Search code examples
rfilterdplyrdata.tabledrop

Is there a dplyr::filter() parameter equivalent to nomatch option in data.table?


With data.table

You can choose between either nomatch=NA:

DT[c("A", "D"), on = "V4", nomatch = NA] #returns a row with "D" even if not found

or nomatch=NULL (same behaviour as dplyr):

DT[c("A", "D"), on = "V4", nomatch = 0] # keep only rows found in V4

With dplyr

filter(DF, V4 %in% c("A", "D")) # we only have equivalent to *nomatch=0* behaviour.

Solution

  • This returns the same output of DT[c("A", "D"), on = "V4", nomatch = NA]

    df %>% right_join(data.frame(V4 = c("A", "D")), "V4")
    

    Reproducible example:

    library(dplyr)
    df <- data.frame(V4 = LETTERS[c(1,1,2,2,3)], V3 = 1:5)
    df %>% right_join(data.frame(V4 = c("A", "D")), "V4")
    #>   V4 V3
    #> 1  A  1
    #> 2  A  2
    #> 3  D NA
    
    library(data.table)
    DT <- as.data.table(df)
    DT[c("A", "D"), on = "V4", nomatch = NA]
    #>    V4 V3
    #> 1:  A  1
    #> 2:  A  2
    #> 3:  D NA