If you have a data.frame with factors that do not include NAs as levels you can filter your data without issues.
set.seed(123)
df=data.frame(a = factor(as.character(c(1, 1, 2, 2, 3, NA,3,NA)),exclude=NULL),
b= runif(8))
#str(df)
df[df$a==3,]
# a b
# 5 3 0.9404673
# 7 3 0.5281055
The issues appears if you need to filter by the NA level. The following does not work:
df[df$a==NA,]
df[df$a=="NA",]
df[is.na(df$a),]
The only way I found is converting the factor to numeric and compare it to the number of levels.
df[as.numeric(df$a)==4,]
# a b
#6 <NA> 0.0455565
#8 <NA> 0.8924190
Is there any other more intuitive/elegant way to get the same result?