1

I have a list of matrix and some of them are NA, like the following:

listToCheck <- list(NA, matrix(0,nrow = 2, ncol = 2))

and I would like to write a code in Rcpp to check if any element of the list is NA or not.

I tried the two following,

// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;

// [[Rcpp::export]]
bool checkNa(int i, List elemInCluster){

   arma::mat matrix = elemInCluster[i];

   if(R_IsNA(matrix(0,0))){
     return true;
   }

   return false;
}

but it doesn't work as when I try checkNa(0, listToCheck) it gives Error in checkNa(0, listToCheck) : Not a matrix. as I try to access the first element even when the matrix is NA.

As R_IsNA works on double, is there any way to check that the matrix is NA without having to access one of its elements?

F. Privé
  • 11,423
  • 2
  • 27
  • 78
adaien
  • 1,932
  • 1
  • 12
  • 26
  • The function ```arma::is_finite``` doesn't work on ```elemInCluster[i]```. Please explain better why this should be a duplicate. – adaien Sep 11 '17 at 09:50
  • Detectecting `NA`, `NaN` and `NULL` has been discussed numerous times. – Dirk Eddelbuettel Sep 11 '17 at 11:32
  • And a list element cannot be NA. That would be a 1x1 matrix with one NA element. Maybe OP meant list element i be NULL instead. We cannot tell as the question is poorly phrased and has not reproducible example. – Dirk Eddelbuettel Sep 11 '17 at 11:48
  • I gave the object ```listToCheck``` to check, so the question is perfectly reproducible. Moreover the element has an element which is NA but it is neither a 1x1 matrix nor a NULL element, as opposed to what you said. – adaien Sep 11 '17 at 14:45
  • Detecting ```NA``` might have been discussed, but not how to check ```NA``` on the element of a list as in OP – adaien Sep 11 '17 at 14:46
  • Re-read all parts of what I wrote three hours ago. – Dirk Eddelbuettel Sep 11 '17 at 14:51
  • Probably you refer to the case where ```listToCheck``` is initialized as ```list(as.matrix(NA), matrix(0,nrow = 2, ncol = 2))```, in that case the answer to the question is easy. But as the question stands, the answer is not clear as none of the functions you suggested work on ```SEXP``` or ```RObject```. – adaien Sep 11 '17 at 14:55
  • 1
    The long and the short of it is that there is no NA for a list type. Int, Numeric, Char, Logical ... can be NA. A list can only be null, or contain an int, numeric, char ... vector which may or may not have a corresponding NA _of that type+. So you start by a wrong example, that is the problem with the lack of a clear reproducible example. – Dirk Eddelbuettel Sep 11 '17 at 16:00
  • Now I understand, thank you. I will change my objects to 1x1 matrix with NA as it's probably a better practice. – adaien Sep 11 '17 at 16:22
  • 1
    It is not a trivial problem the way you posed (with the slight misunderstanding of thinking a list could be NA) because it sits at the difference between untyped R, and strongly typed C++. – Dirk Eddelbuettel Sep 11 '17 at 16:24

1 Answers1

-1

Using try-catch like in this example seems to work:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
bool checkNa(int i, List elemInCluster){

  try {
    return R_IsNA(elemInCluster[i]);
  } catch(...) {
    return false;
  }
}


> checkNa(0, listToCheck)
[1] TRUE

> checkNa(1, listToCheck)
[1] FALSE
F. Privé
  • 11,423
  • 2
  • 27
  • 78
  • There is a really bad hack. See the better answers at the already-answered earlier question. – Dirk Eddelbuettel Sep 11 '17 at 04:16
  • 2
    `arma::is_finite(elemInCluster[i])` won't compile, so I don't know how that duplicate you marked would be a better answer for the OP. – F. Privé Sep 11 '17 at 06:35
  • Detectecting `NA`, `NaN` and `NULL` has been discussed numerous times. – Dirk Eddelbuettel Sep 11 '17 at 11:32
  • 1
    This is a _very_ bad approach to take @F.Privé. In particular, even if `NA` was defined for `Rcpp::List`, this only covers 1/2 of the definition of `NA`. See [Finite, Infinite, Missingness, and NaN Detection](http://thecoatlessprofessor.com/programming/rcpp/unofficial-rcpp-api-docs/#sugar-nan) for details. – coatless Sep 11 '17 at 22:09
  • @coatless not sure what you mean – F. Privé Sep 11 '17 at 22:14