在R中使用NA值组合删除行的高效方法 [英] Efficient method to subset drop rows with NA values in R

查看:132
本文介绍了在R中使用NA值组合删除行的高效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景
在运行逐步模型选择之前,我需要删除我的任何模型项的缺失值。在我的模型中有相当多的术语,因此,我需要查找NA值(并删除任何这些向量中具有NA值的任何行)的相当多的向量。但是,还有一些向量包含不想用作删除行的条款/标准的NA值。

Background Before running a stepwise model selection, I need to remove missing values for any of my model terms. With quite a few terms in my model, there are therefore quite a few vectors that I need to look in for NA values (and drop any rows that have NA values in any of those vectors). However, there are also vectors that contain NA values that I do not want to use as terms / criteria for dropping rows.

问题
如何从包含任何矢量列表的NA值的数据帧中删除行?我目前正在使用一系列长的系列的笨重方法!is.na的

Question How do I drop rows from a dataframe which contain NA values for any of a list of vectors? I'm currently using the clunky method of a long series of !is.na's

> my.df[!is.na(my.df$termA)&!is.na(my.df$termB)&!is.na(my.df$termD),]

但我确信有一个更优雅的方法。

but I'm sure that there is a more elegant method.

推荐答案

dat 成为数据框,并且 cols 列名称向量或列号码。然后你可以使用

Let dat be a data frame and cols a vector of column names or column numbers of interest. Then you can use

dat[!rowSums(is.na(dat[cols])), ]

排除所有行至少有一个 NA

to exclude all rows with at least one NA.

这篇关于在R中使用NA值组合删除行的高效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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