Back
What is the fastest way to detect if a vector has at least 1 NA in R? I've been using:
sum( is.na( data ) ) > 0
But that requires examining each element, coercion, and the sum function.
You can use the anyNA() function that for atomic vectors will stop after the first NA is encountered instead of going through the whole vector. i.e
x <- y <- runif(1e8) x[1e5] <- NA y[1e8] <- NA
x <- y <- runif(1e8)
x[1e5] <- NA
y[1e8] <- NA
library("microbenchmark") microbenchmark::microbenchmark(any(is.na(x)), anyNA(x), any(is.na(y)), anyNA(y), times=10)Unit: microseconds expr min lq mean median uq max neval any(is.na(x)) 172072.058 175931.968 238283.1398 186712.6940 359786.850 379623.999 10 anyNA(x) 88.178 94.152 100.8076 95.4315 98.987 142.507 10 any(is.na(y)) 256447.358 258283.163 292296.5928 259401.8820 266702.432 446019.571 10 anyNA(y) 91422.695 91562.641 98660.3245 92002.9610 102395.136 128808.355 10
library("microbenchmark")
microbenchmark::microbenchmark(any(is.na(x)), anyNA(x), any(is.na(y)), anyNA(y), times=10)
Unit: microseconds
expr min lq mean median uq max neval
any(is.na(x)) 172072.058 175931.968 238283.1398 186712.6940 359786.850 379623.999 10
anyNA(x) 88.178 94.152 100.8076 95.4315 98.987 142.507 10
any(is.na(y)) 256447.358 258283.163 292296.5928 259401.8820 266702.432 446019.571 10
anyNA(y) 91422.695 91562.641 98660.3245 92002.9610 102395.136 128808.355 10
31k questions
32.8k answers
501 comments
693 users