How to filter data without losing NA rows using dplyr

后端 未结 2 785
暖寄归人
暖寄归人 2020-12-11 17:24

How to subset data in R without losing NA rows?

The post above subsets using logical indexing. Is there a way to do it in dplyr?

Also,

相关标签:
2条回答
  • 2020-12-11 17:52

    The documentation for dplyr::filter says... "Unlike base subsetting, rows where the condition evaluates to NA are dropped."

    NA != "str" evaluates to NA so is dropped by filter.

    !grepl("str", NA) returns TRUE, so is kept.

    If you want filter to keep NA, you could do filter(is.na(col)|col!="str")

    0 讨论(0)
  • 2020-12-11 17:53

    If you want to keep NAs created by the filter condition you can simply turn the condition NAs into TRUEs using replace_na from tidyr.

    a <- data.frame(col = c("hello", NA, "str"))
    a %>% filter((col != "str") %>% replace_na(TRUE))
    
    0 讨论(0)
提交回复
热议问题