Created
September 17, 2021 15:27
-
-
Save jmbarbone/d93a17ecc59eaaae4cab0375b265d583 to your computer and use it in GitHub Desktop.
some bench marking for removing NA values in a vector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foo1 <- function(x) { | |
x[!is.na(x)] | |
} | |
foo2 <- function(x) { | |
nas <- seq_along(x)[is.na(x)] | |
if (length(nas)) x[-nas] else x | |
} | |
foo3 <- function(x) { | |
nas <- is.na(x) | |
if (any(nas)) x[!nas] else x | |
} | |
foo4 <- function(x) { | |
if (anyNA(x)) x[-which(is.na(x))] else x | |
} | |
foo5 <- function(x) { | |
if (anyNA(x)) x[!is.na(x)] else x | |
} | |
x <- runif(1e5) | |
y <- x | |
y[sample(1e5, 1e4)] <- NA | |
bench::mark(foo1(x), foo2(x), foo3(x), foo4(x), foo5(x)) | |
bench::mark(foo1(y), foo2(y), foo3(y), foo4(y), foo5(y)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment