删除缺少值的行的最快方法? [英] Fastest way to drop rows with missing values?

查看:108
本文介绍了删除缺少值的行的最快方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用大型数据集 x 。我想删除 x 的一列中的一个或多个列中缺少的 x 行设置由字符向量 varcols 指定。

I'm working with a large dataset x. I want to drop rows of x that are missing in one or more columns in a set of columns of x, that set being specified by a character vector varcols.

到目前为止,我尝试了以下操作:

So far I've tried the following:

require(data.table)
x <- CJ(var1=c(1,0,NA),var2=c(1,0,NA))
x[, textcol := letters[1:nrow(x)]]
varcols <- c("var1","var2")

x[, missing := apply(sapply(.SD,is.na),1,any),.SDcols=varcols]
x <- x[!missing]

有更快的方法吗?
谢谢。

Is there a faster way of doing this? Thanks.

推荐答案

这应该比使用 apply

x[rowSums(is.na(x[, varcols, with = FALSE])) == 0, ]
#    var1 var2 textcol
# 1:    0    0       e
# 2:    0    1       f
# 3:    1    0       h
# 4:    1    1       i

这篇关于删除缺少值的行的最快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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