Fastest way to detect if vector has at least 1 NA?

后端 未结 6 1000
南旧
南旧 2020-12-23 14:50

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

6条回答
  •  隐瞒了意图╮
    2020-12-23 15:12

    We mention this in some of our Rcpp presentations and actually have some benchmarks which show a pretty large gain from embedded C++ with Rcpp over the R solution because

    • a vectorised R solution still computes every single element of the vector expression

    • if your goal is to just satisfy any(), then you can abort after the first match -- which is what our Rcpp sugar (in essence: some C++ template magic to make C++ expressions look more like R expressions, see this vignette for more) solution does.

    So by getting a compiled specialised solution to work, we do indeed get a fast solution. I should add that while I have not compared this to the solutions offered in this SO question here, I am reasonably confident about the performance.

    Edit And the Rcpp package contains examples in the directory sugarPerformance. It has an increase of the several thousand of the 'sugar-can-abort-soon' over 'R-computes-full-vector-expression' for any(), but I should add that that case does not involve is.na() but a simple boolean expression.

提交回复
热议问题