data.table 中是否有等同于 nomatch 选项的 dplyr::filter() 参数?
Is there a dplyr::filter() parameter equivalent to nomatch option in data.table?
和data.table
您可以选择 nomatch=NA:
DT[c("A", "D"), on = "V4", nomatch = NA] #returns a row with "D" even if not found
或nomatch=NULL(与dplyr行为相同):
DT[c("A", "D"), on = "V4", nomatch = 0] # keep only rows found in V4
使用 dplyr
filter(DF, V4 %in% c("A", "D")) # we only have equivalent to *nomatch=0* behaviour.
这returns与DT[c("A", "D"), on = "V4", nomatch = NA]
的输出相同
df %>% right_join(data.frame(V4 = c("A", "D")), "V4")
可重现的例子:
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
和data.table
您可以选择 nomatch=NA:
DT[c("A", "D"), on = "V4", nomatch = NA] #returns a row with "D" even if not found
或nomatch=NULL(与dplyr行为相同):
DT[c("A", "D"), on = "V4", nomatch = 0] # keep only rows found in V4
使用 dplyr
filter(DF, V4 %in% c("A", "D")) # we only have equivalent to *nomatch=0* behaviour.
这returns与DT[c("A", "D"), on = "V4", nomatch = NA]
df %>% right_join(data.frame(V4 = c("A", "D")), "V4")
可重现的例子:
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