You can try the following:
myRowSums <- rowSums(is.na(s) | s == 0) myColSums <- colSums(is.na(s) | s == 0) sSmall <- s[which(myRowSums != ncol(s)), which(myColSums != nrow(s))]
It works for the next data set to remove all columns and rows that are completely composed of 0 and NA.
s <- data.frame(a=c(0, rnorm(5), 0), b=c(0, rnorm(2), NA, NA,1, NA), c=c(rep(c(0,NA), 3), 0))
source share