Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
benchmark is.na
library(data.table)
fis.na = data.table:::fis.na
N = 1e8
set.seed(108)
N_na = 1e3
N_nan = 1e3
# integer
x = sample(N)
x[sample(N, N_na)] = NA
system.time(ans1<-is.na(x))
system.time(ans2<-fis.na(x))
identical(ans1, ans2)
# raw
x = as.raw(1:N)
system.time(ans1<-is.na(x))
system.time(ans2<-fis.na(x))
identical(ans1, ans2)
# string
x = paste0("id", 1:N)
x[sample(N, N_na)] = NA
system.time(ans1<-is.na(x))
system.time(ans2<-fis.na(x))
identical(ans1, ans2)
# double
# there is no function for "NA but not NaN"
is.na(NaN)
is.nan(NA)
is.na.only = function(x) is.na(x) & !is.nan(x)
x = runif(N)
x[sample(N, N_na)] = NA
x[sample(N, N_nan)] = NaN
system.time(ans1<-is.na(x))
system.time(ans2<-fis.na(x))
identical(ans1, ans2)
x = runif(N)
x[sample(N, N_na)] = NA
x[sample(N, N_nan)] = NaN
system.time(ans1<-is.na.only(x))
system.time(ans2<-fis.na(x, NA))
identical(ans1, ans2)
x = runif(N)
x[sample(N, N_na)] = NA
x[sample(N, N_nan)] = NaN
system.time(ans1<-is.nan(x))
system.time(ans2<-fis.na(x, NaN))
identical(ans1, ans2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment