A function for filtering, grouping and mutating data with dplyr functions. Basic pipe sequence works great outside a function, that is where I use the true column names. Put it in a function where the column name is a variable and some of the functions work but some don't most notably dplyr::filter(). For example:
var1 <- c('yes', NA, NA, 'yes', 'yes', NA, NA, NA, 'yes', NA, 'no', 'no', 'no', 'maybe', NA, 'maybe', 'maybe', 'maybe')
var2 <- c(1:18)
df <- data.frame(var1, var2)
This works fine (i.e. filters NA's):
df%>%filter(!is.na(var1))
...but this doesn't:
x <- "var1"
df%>%filter(!is.na(x))
...but this does:
df%>%select(x)
It's NA's that need to be filtered out specifically.
Tried get("x"), no good, and slicing:
df[!is.na(x),]
...no good, either.
Any ideas on how to pass a variable to filter inside (or outside) a function and why a variable is working with other dplyr functions?