从 data.table 中删除仅包含 NA 的行 [英] Remove lines with only NAs from data.table

查看:17
本文介绍了从 data.table 中删除仅包含 NA 的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 data.table 中删除仅包含 NA 的行.

I want to remove the lines from a data.table that only contain NAs.

> tab = data.table(A = c(1, NA, 3), B = c(NA, NA, 3))
> tab
    A  B
1:  1 NA
2: NA NA
3:  3  3

通常我会用 apply(dat, 1, ...) 来做,不幸的是它不适用于 data.table 但它导致我这个不优雅的解决方案:

Normally I would do it with apply(dat, 1, ...) which unfortunately does not work on a data.table but it leads me to this inelegant solution:

> tab[apply(as.data.frame(tab), 1, function(x) !all(is.na(x))), ]
   A  B
1: 1 NA
2: 3  3

如何在不知道列名的情况下以最快的方式实现这一点?

How can this be achieved the fastest way without knowing the column names?

推荐答案

我们可以使用 Reduceis.na&

We can use Reduce with is.na and &

tab[!Reduce(`&`, lapply(tab, is.na))]
#   A  B
#1: 1 NA
#2: 3  3

<小时>

或者一个紧凑但不那么有效的方法是


Or a compact but not so efficient approach would be

tab[rowSums(!is.na(tab)) != 0L]

此外,正如 @Frank 所评论的,一种基于联接的方法,

Also, as commented by @Frank, a join based approach,

tab[!tab[NA_integer_], on = names(tab)]

这篇关于从 data.table 中删除仅包含 NA 的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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