I want to remove all columns that are all NA
. I have two solutions as shown below, but they both use looping structures under the hood.
I was wondering if there is a faster solution that does NOT require using ...apply
or for
loops?
PS. if I wanted to remove all rows with all NA
I could avoid loops like so: r[rowSums(is.na(r)) != ncol(r), ]
but can a similar solution apply to columns?
r <- data.frame(AA = c(1, NA, 3), BB = c(NA, NA, NA), CC = c(3, NA, 5) )
r[sapply(r, function(r) !all(is.na(r)))] # Solution 1
Filter(function(x) !all(is.na(x)), r) # Solution 2