<div dir="ltr"><div>What is correct way to represent NA in a template? I have been successfully using NA_REAL (see example below) but I am not sure if this is the proper method.</div><div>Thanks.</div><div><br></div><div>/* template_NA.cpp:</div><div>Experiment with use of NA in templates in Rcpp. Change first member of a vector to NA.</div><div>*/</div><div><br></div><div>#include <Rcpp.h></div><div>using namespace Rcpp;</div><div><br></div><div>template<typename T></div><div>T first2NA(T vec) { //Set first el of vector to NA.</div><div><span class="" style="white-space:pre">   </span>int n = vec.size();</div><div><span class="" style="white-space:pre">        </span>if (n == 0) return vec;</div><div><span class="" style="white-space:pre">    </span>vec[0] = NA_REAL;</div><div><span class="" style="white-space:pre">  </span>return(vec);</div><div>}</div><div><br></div><div>// [[Rcpp::export]]</div><div>LogicalVector first2NA_logi(LogicalVector vec) {return first2NA(vec);}</div><div>// [[Rcpp::export]]</div><div>IntegerVector first2NA_int(IntegerVector vec) {return first2NA(vec);}</div><div>// [[Rcpp::export]]</div><div>NumericVector first2NA_num(NumericVector vec) {return first2NA(vec);}</div><div>// [[Rcpp::export]]</div><div>CharacterVector first2NA_char(CharacterVector vec) {return first2NA(vec);}</div><div><br></div><div>/***R</div><div>vec_logi = sample(c(T, F), 5, rep=T)</div><div>vec_logi</div><div>first2NA_logi(vec_logi)</div><div>vec_int = 1:5</div><div>vec_int</div><div>first2NA_int(vec_int)</div><div>vec_num = rnorm(5)</div><div>vec_num</div><div>first2NA_num(vec_num)</div><div>vec_char = letters[1:5]</div><div>vec_char</div><div>first2NA_char(vec_char)</div><div>*/</div><div><br></div></div>