简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM