NA Action of Filter of Dplyr

filter in dplyr will drop NAs when we filter a variable bigger or less than certain value.

require(dplyr)
## Loading required package: dplyr
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
test <- data.frame(value = c(rep(NA,3), -3:3))

The result of filter function

dplyr::filter(test, value > 0)
##   value
## 1     1
## 2     2
## 3     3

The result of base functions

test$value[test$value > 0]
## [1] NA NA NA  1  2  3

Conclusion

If we use filter, the result would drop NA observations.

if we use base functions, the NAs are remained.

comments powered by Disqus