r - Check if element of list of matrix are na in rcpp -
this question has answer here:
- rcpp function check if missing value 2 answers
i have list of matrix , of them na, following:
listtocheck <- list(na, matrix(0,nrow = 2, ncol = 2))
and write code in rcpp check if element of list na or not.
i tried 2 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 doesn't work when try checkna(0, listtocheck)
gives error in checkna(0, listtocheck) : not matrix.
try access first element when matrix na.
as r_isna works on double, there way check matrix na without having access 1 of elements?
using try-catch in this example seems 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
Comments
Post a Comment