[This article was first published on R – Statistical Odds & Ends, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In R, we can subset a data frame df easily by putting the conditional in square brackets after df. For example, if I want all the rows in df which have value equal to 1 in the column colA, all I have to do is

df[df$colA == 1, ]  Recently, I realized that this approach can be problematic when there are NAs present in the data! For example, let df be the following data frame: df <- data.frame(colA = c(1, 2, 3, NA, NA), colB = c("a", "b", "c", "d", NA)) df # co1A colB # 1 1 a # 2 2 b # 3 3 c # 4 NA d # 5 NA  If I want to pull out all rows such that the value in colA is equal to 2, I would use the following code expecting just one row to be returned: df[df$colA == 2, ]

#      colA colB
# 2       2    b
# NA     NA
# NA.1   NA


All the rows with NA in colA also got included, and all the values in those rows got converted into NAs! To avoid these rows from being added, we have to explicitly check that the values are not NAs:

df[!is.na(df$colA) & df$colA == 2, ]

# colA colB
# 2    2    b


dplyr acts a bit differently in that it removes NAs by default and so you do not have to check for them:

library(dplyr)
df %>% filter(colA == 2)

# colA colB
# 2    2    b