#includeusing namespace Rcpp; // [[Rcpp::export]] NumericVector add(NumericVector x, NumericVector y) { int n = x.size(); NumericVector z(n); for(int i = 0; i < n; ++i) { z[i] = x[i] + y[i]; } return z; }
library(Rcpp) sourceCpp("add.cpp") x <- c(1.0, 2.0, 3.0) y <- c(4.0, 5.0, 6.0) add(x, y)This will output: `[1] 5 7 9`