How to filter data without losing NA rows using dplyr

后端 未结 2 784
暖寄归人
暖寄归人 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: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))
    

提交回复
热议问题