具有多列条件的dplyr过滤器 [英] dplyr filter with condition on multiple columns

查看:62
本文介绍了具有多列条件的dplyr过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个虚拟数据:

father<- c(1, 1, 1, 1, 1)
mother<- c(1, 1, 1, NA, NA) 
children <- c(NA, NA, 2, 5, 2) 
cousins   <- c(NA, 5, 1, 1, 4) 


dataset <- data.frame(father, mother, children, cousins)  
dataset


father  mother  children cousins
1      1       NA      NA
1      1       NA       5
1      1        2       1
1     NA        5       1
1     NA        2       4

我要过滤此行:

  father  mother  children cousins
    1      1       NA      NA

我可以做到使用:

test <- dataset %>% 
filter(father==1 & mother==1) %>%
filter (is.na(children)) %>%
filter (is.na(cousins))
test  

我的问题:
我有很多专栏,例如祖父,叔叔,叔叔2,叔叔e3,我想避免这样的事情:

My question : I have many columns like grand father, uncle1, uncle2, uncle3 and I want to avoid something like that:

  filter (is.na(children)) %>%
  filter (is.na(cousins)) %>%
  filter (is.na(uncle1)) %>%
  filter (is.na(uncle2)) %>%
  filter (is.na(uncle3)) 
  and so on...

如何使用dplyr来过滤所有带有na的列(father == 1& mother == 1)

How can I use dplyr to say filter all the column with na (except father==1 & mother==1)

推荐答案

可能的 dplyr (版本> = 0.5.0.9004)解决方案是:

A possible dplyr(version >= 0.5.0.9004) solution is:

# > packageVersion('dplyr')
# [1] ‘0.5.0.9004’

dataset %>%
    filter(!is.na(father), !is.na(father)) %>%
    filter_at(vars(-father, -mother), all_vars(is.na(.)))

说明:


  • vars(-father,-mother) :选择除父亲母亲之外的所有列。

  • all_vars(is.na(。)):保留 is.na 为<$ c $的行c>对所有所选列都为真。

  • vars(-father, -mother): select all columns except father and mother.
  • all_vars(is.na(.)): keep rows where is.na is TRUE for all the selected columns.

注意:<$如果行 is.na ,则应使用c $ c> any_vars 代替 all_vars 保留任何列的 TRUE

note: any_vars should be used instead of all_vars if rows where is.na is TRUE for any column are to be kept.

这篇关于具有多列条件的dplyr过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆