使用dplyr对数据框架中的完整案例进行过滤(案例删除) [英] filter for complete cases in data.frame using dplyr (case-wise deletion)

查看:200
本文介绍了使用dplyr对数据框架中的完整案例进行过滤(案例删除)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用dplyr可以对数据框架进行过滤吗?当然,$ code> complete.cases 列出所有变量的作品。但是这是一个很冗长的变量b)当变量名称不知道(例如在处理任何data.frame的函数中)时,不可能。

Is it possible to filter a data.frame for complete cases using dplyr? complete.cases with a list of all variables works, of course. But that is a) verbose when there are a lot of variables and b) impossible when the variable names are not known (e.g. in a function that processes any data.frame).

library(dplyr)
df = data.frame(
    x1 = c(1,2,3,NA),
    x2 = c(1,2,NA,5)
)

df %.%
  filter(complete.cases(x1,x2))


推荐答案

尝试这样:

df %>% na.omit

或此:

df %>% filter(complete.cases(.))

ADDED 更新以反映最新版本的dplyr和评论。

ADDED Have updated to reflect latest version of dplyr and comments.

这篇关于使用dplyr对数据框架中的完整案例进行过滤(案例删除)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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