How can I pass an NA value from Rcpp to R in a 64 bit vector?
My first approach would be:
// [[Rcpp::export]]
Rcpp::NumericVector foo() {
Rcpp::NumericVector res(2);
int64_t val = 1234567890123456789;
std::memcpy(&(res[0]), &(val), sizeof(double));
res[1] = NA_REAL;
res.attr("class") = "integer64";
return res;
}
But it yields
#> foo()
integer64
[1] 1234567890123456789 9218868437227407266
I need to get
#> foo()
integer64
[1] 1234567890123456789 <NA>
Alright, I think I found an answer... (not beautiful, but working).
Short Answer:
which results in
Longer Answer
Inspecting how
bit64stores anNACreated on 2020-04-23 by the reprex package (v0.3.0)
we see that it is a
10000.... This can be recreated inRcppwithint64_t val = 1ULL << 63;. Usingmemcpy()instead of a simple assign with=ensures that no bits are changed!