Skip to content

Consider adding a drop_na verb #194

@jankatins

Description

@jankatins

[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  3

I can clean the above up and submit a PR if such a verb would be considered.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions