R - fastest way to detect if vector has at least 1 NA? -
wondering fastest way detect if vector has @ least 1 na? i've been using:
sum( is.na( data ) ) > 0
requires examining each element, coercion , , sum function.
the newer versions of r have anyna()
option. on atomic vectors stop after first na instead of going through entire vector case any(is.na())
. borrowing joran's example:
x <- y <- runif(1e7) x[1e4] <- na y[1e7] <- na microbenchmark::microbenchmark(any(is.na(x)), anyna(x), any(is.na(y)), anyna(y), times=10) # unit: microseconds # expr min lq mean median uq # any(is.na(x)) 13444.674 13509.454 21191.9025 13639.3065 13917.592 # anyna(x) 6.840 13.187 13.5283 14.1705 14.774 # any(is.na(y)) 165030.942 168258.159 178954.6499 169966.1440 197591.168 # anyna(y) 7193.784 7285.107 7694.1785 7497.9265 7865.064
notice how substantially faster when modify last value of vector. big part of savings, in addition stopping early, don't need create , allocate memory entire logical vector size of our numeric vector.
Comments
Post a Comment