[Rcpp-devel] NA in templates

Romain Francois romain at r-enthusiasts.com
Wed Sep 9 08:26:43 CEST 2015


You can use T::get_na() 

Romain

> Le 9 sept. 2015 à 08:24, Rguy <rguy at 123mail.org> a écrit :
> 
> 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)
> */
> 
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel



More information about the Rcpp-devel mailing list