从data.table中删除只有NAs的行 [英] Remove lines with only NAs from data.table
本文介绍了从data.table中删除只有NAs的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要从只包含NA的data.table中删除这些行。
> tab = data.table(A = c(1,NA,3),B = c(NA,NA,3))
&标签
AB
1:1 NA
2:NA NA
3:3 3
通常我会使用 apply(dat,1,...)
这不幸的是对一个data.table无效,但它导致这个不起眼的解决方案:
>标签[apply(as.data.frame(tab),1,function(x)!all(is.na(x))],
AB
1:1 NA
2 :3 3
如何在不知道列名的情况下实现最快的方式?
解决方案
我们可以使用减少
和 is.na
和
&
`&`,lapply(tab,is.na))]
#AB
#1:1 NA
#2:3 3
或者紧凑但不那么高效的方法是
tab [rowSums(!is.na(tab))!= 0L]
此外,由@Frank评论为基于连接的方法,
NA_integer_],on = names(tab)]
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
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?
解决方案We can use
Reduce
withis.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]
Also, as commented by @Frank, a join based approach,
tab[!tab[NA_integer_], on = names(tab)]
这篇关于从data.table中删除只有NAs的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文