[Rcpp-devel] NA in templates

Rguy rguy at 123mail.org
Wed Sep 9 08:24:22 CEST 2015


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.
Thanks.

/* template_NA.cpp:
Experiment with use of NA in templates in Rcpp. Change first member of a
vector to NA.
*/

#include <Rcpp.h>
using namespace Rcpp;

template<typename T>
T first2NA(T vec) { //Set first el of vector to NA.
int n = vec.size();
if (n == 0) return vec;
vec[0] = NA_REAL;
return(vec);
}

// [[Rcpp::export]]
LogicalVector first2NA_logi(LogicalVector vec) {return first2NA(vec);}
// [[Rcpp::export]]
IntegerVector first2NA_int(IntegerVector vec) {return first2NA(vec);}
// [[Rcpp::export]]
NumericVector first2NA_num(NumericVector vec) {return first2NA(vec);}
// [[Rcpp::export]]
CharacterVector first2NA_char(CharacterVector vec) {return first2NA(vec);}

/***R
vec_logi = sample(c(T, F), 5, rep=T)
vec_logi
first2NA_logi(vec_logi)
vec_int = 1:5
vec_int
first2NA_int(vec_int)
vec_num = rnorm(5)
vec_num
first2NA_num(vec_num)
vec_char = letters[1:5]
vec_char
first2NA_char(vec_char)
*/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20150909/30febf23/attachment.html>


More information about the Rcpp-devel mailing list