I am new to Rcpp and experimenting it at the moment. In a toy code, I tried to replace a even number in a vector by the number times 2.
In one case I used NumericVector as input and output data types, and in another case I used IntegerVector.
#include <Rcpp.h>
using namespace Rcpp;
using namespace std;
// [[Rcpp::export]]
NumericVector replaceEven(NumericVector x) {
for (int i = 0; i < x.length(); i++) {
if (fmod(x[i], 2) == 0) {
x[i] = x[i] * 2;
}
}
return x;
}
sourceCpp("RcppReplaceEven.cpp")
> y = c(1,2,3,4)
> replaceEven(y)
[1] 1 4 3 8
> y
[1] 1 4 3 8
However, after replacing NumericVector by IntegerVector, the result is
> y = c(1,2,3,4)
> replaceEven(y)
[1] 1 4 3 8
> y
[1] 1 2 3 4
Just wondering why this is the case.