除去第一列以外具有所有NA的行 [英] Remove rows which have all NA's, apart from the first column

查看:93
本文介绍了除去第一列以外具有所有NA的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个data.table,它是通过使用以下两个面板观察值之间的差异来形成的:

I have a data.table which was formed by taking the differences between two panel observations using:

tab <- tab[,
   lapply(.SD, function(x) x - shift(x)), 
   by = A, 
   .SDcols = (sapply(tab, is.numeric))
  ]

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

我想使用以下 answer :

tab <- tab [!Reduce(`&`, lapply(tab , is.na))]

删除行1和3,但这不起作用,因为第一列不是NA.我该如何修改代码来解决这个问题?

to remove rows 1 and 3, but this does not work because the first column is not NA. How can I adapt the code to solve this?

所需结果:

    A  B  C  D
1:  1  2  NA 3
2:  2  1  2  2 

推荐答案

在这种情况下,我们可以在.SDcols

In this case we can specify the columns in .SDcols

tab[tab [, !Reduce(`&`, lapply(.SD , is.na)), .SDcols = 2:ncol(tab)]]

这篇关于除去第一列以外具有所有NA的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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