Remove rows which have all NA's, apart from the first column [duplicate]

六眼飞鱼酱① 提交于 2019-12-24 11:25:02

问题


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 

I would like to use this answer:

tab <- tab [!Reduce(`&`, lapply(tab , is.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?

Desired outcome:

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

回答1:


In this case we can specify the columns in .SDcols

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



回答2:


tab[tab[, rowSums(!is.na(.SD)) > 1, .SDcols = -1]]


来源:https://stackoverflow.com/questions/56871943/remove-rows-which-have-all-nas-apart-from-the-first-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!