If you also have true missing values (which are not related to factor levels) ...
DF = data.frame( x = factor(c("A", "B", NA), levels=c("A", NA), exclude=NULL), v = 1:3 )
Line 3 x is NA , while line 2 is a true missing value.
To get only row 3, you can make a connection to data.table ...
library(data.table) setDT(DF) merge(DF, data.table(x = factor(NA_character_, exclude=NULL)))
Or somewhat more inconvenient in dplyr:
dplyr::right_join(DF, data.frame(x = factor(NA_character_, levels=levels(DF$x), exclude=NULL))) # Joining, by = "x" # xv # 1 <NA> 3
I could not find any way to get here, except for the crazy ...
wv = which(is.na(levels(DF$x))) DF[ !is.na(DF$x) & as.integer(DF$x) == wv, ]
Frank source share