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