-
Notifications
You must be signed in to change notification settings - Fork 419
Closed
Description
[forwarded from https://github.com/tidyverse/dplyr/issues/1797 as per @hadley: "This feels more like a tidyr verb to me."]
pandas has the pandas.DataFrame.dropna() which lets you drop rows which have NAs in it but also let you specify subset of columns (-> subset) to be looked at for NAs.
A similar dplyr verb would be
drop_na <- function(data, ...){
if (missing(...)){
f = complete.cases(data)
} else {
f <- complete.cases(select_(data, .dots = lazyeval::lazy_dots(...)))
}
filter(data, f)
}Examples:
> df <- data.frame(a=c(1,2,3,4,NA), b=c(NA,1,2,3,4), ac=c(1,2,NA,3,4))
> df %>% drop_na(a,b)
a b ac
1 2 1 2
2 3 2 NA
3 4 3 3
> df %>% drop_na(starts_with("a"))
a b ac
1 1 NA 1
2 2 1 2
3 4 3 3
> df %>% drop_na()
a b ac
1 2 1 2
2 4 3 3I can clean the above up and submit a PR if such a verb would be considered.
Metadata
Metadata
Assignees
Labels
No labels