## Benchmarking

@DavidArenburg suggested a number of alternatives. Here's a simple benchmarking of them.

`library(tidyverse) library(microbenchmark) n <- 100 dat <- tibble(a = rep(c(1, 2, NA), n), b = rep(c(1, 1, NA), n)) f1 <- function(dat) { na <- dat %>% rowwise() %>% do(tibble(na = !all(is.na(.)))) %>% .$na filter(dat, na) } f2 <- function(dat) { dat %>% filter(rowSums(is.na(.)) != ncol(.)) } f3 <- function(dat) { dat %>% filter(rowMeans(is.na(.)) < 1) } f4 <- function(dat) { dat %>% filter(Reduce(`+`, lapply(., is.na)) != ncol(.)) } f5 <- function(dat) { dat %>% mutate(indx = row_number()) %>% gather(var, val, -indx) %>% group_by(indx) %>% filter(sum(is.na(val)) != n()) %>% spread(var, val) } # f1 is too slow to be included! microbenchmark(f2 = f2(dat), f3 = f3(dat), f4 = f4(dat), f5 = f5(dat)) `

Using `Reduce`

and `lapply`

appears to be the fastest:

`> microbenchmark(f2 = f2(dat), f3 = f3(dat), f4 = f4(dat), f5 = f5(dat)) Unit: microseconds expr min lq mean median uq max neval f2 909.495 986.4680 2948.913 1154.4510 1434.725 131159.384 100 f3 946.321 1036.2745 1908.857 1221.1615 1805.405 7604.069 100 f4 706.647 809.2785 1318.694 960.0555 1089.099 13819.295 100 f5 640392.269 664101.2895 692349.519 679580.6435 709054.821 901386.187 100 `

Using a larger data set `107,880 x 40`

:

`dat <- diamonds # Let every third row be NA dat[seq(1, nrow(diamonds), 3), ] <- NA # Add some extra NA to first column so na.omit() wouldn't work dat[seq(2, nrow(diamonds), 3), 1] <- NA # Increase size dat <- dat %>% bind_rows(., .) %>% bind_cols(., .) %>% bind_cols(., .) # Make names unique names(dat) <- 1:ncol(dat) microbenchmark(f2 = f2(dat), f3 = f3(dat), f4 = f4(dat)) `

`f5`

is too slow so it is also excluded. `f4`

seems to do relatively better than before.

`> microbenchmark(f2 = f2(dat), f3 = f3(dat), f4 = f4(dat)) Unit: milliseconds expr min lq mean median uq max neval f2 34.60212 42.09918 114.65140 143.56056 148.8913 181.4218 100 f3 35.50890 44.94387 119.73744 144.75561 148.8678 254.5315 100 f4 27.68628 31.80557 73.63191 35.36144 137.2445 152.4686 100 `