In R you can select all non-NA
rows from a dataframe via:
set.seed(123)
nadf = data.frame(a = 1:20, b=sample(c("a","b",NA), 20, replace=TRUE))
nadf[complete.cases(nadf),]
The nice thing is that I do not need to specify which columns to check. R will check every value of every row and column if it is NA
. I would now like to do this with dplyr
.
In dplyr
my first reaction would be to use the filter
command. However, through filter
I would need to specify every column that I want to filter on. Assuming that this dataframe has column names a...z
and it would look something like this:
df = df %>% filter( a != NA, b != NA, ..., z != NA)
This is very verbose and I imagine there must be a better way to do this operation in dplyr. Is there?