1

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 
Tom
  • 2,173
  • 1
  • 17
  • 44

2 Answers2

2
tab[tab[, rowSums(!is.na(.SD)) > 1, .SDcols = -1]]
s_baldur
  • 29,441
  • 4
  • 36
  • 69
0

In this case we can specify the columns in .SDcols

tab[tab [, !Reduce(`&`, lapply(.SD , is.na)), .SDcols = 2:ncol(tab)]]
Cath
  • 23,906
  • 5
  • 52
  • 86
akrun
  • 874,273
  • 37
  • 540
  • 662