[Rcpp-devel] Rcpp sugar preserving attributes
Søren Højsgaard
sorenh at math.aau.dk
Sun Jan 4 10:53:57 CET 2015
Dear all,
I have noticed an issue with preserving attributes (or rather attributes that are not preserved). Consider this code where I take the log of an array:
#include <Rcpp.h>
using namespace Rcpp;
//[[Rcpp::export]]
NumericVector fun1(NumericVector x){
return log( x );
}
//[[Rcpp::export]]
NumericVector fun2(NumericVector x){
NumericVector out=clone(x), tmp=log(x);
for (int i=0;i<x.size();++i)out[i] = tmp[i];
return out;
}
/*** R
x <- HairEyeColor; x
fun1(x)
fun2(x)
*/
> fun1(x)
[1] 3.4657359 3.9702919 2.3025851 1.0986123 2.3978953 3.9120230 2.3025851 3.4011974 2.3025851 3.2188758 1.9459101
[12] 1.6094379 1.0986123 2.7080502 1.9459101 2.0794415 3.5835189 4.1896547 2.7725887 1.3862944 2.1972246 3.5263605
[23] 1.9459101 4.1588831 1.6094379 3.3672958 1.9459101 1.6094379 0.6931472 2.6390573 1.9459101 2.0794415
> fun2(x)
, , Sex = Male
Eye
Hair Brown Blue Hazel Green
Black 3.4657359 2.3978953 2.3025851 1.0986123
Brown 3.9702919 3.9120230 3.2188758 2.7080502
Red 2.3025851 2.3025851 1.9459101 1.9459101
Blond 1.0986123 3.4011974 1.6094379 2.0794415
, , Sex = Female
Eye
Hair Brown Blue Hazel Green
Black 3.5835189 2.1972246 1.6094379 0.6931472
Brown 4.1896547 3.5263605 3.3672958 2.6390573
Red 2.7725887 1.9459101 1.9459101 1.9459101
Blond 1.3862944 4.1588831 1.6094379 2.0794415
fun2 is kludgy! Is there a more elegant way of preserving attributes using Rcpp sugar?
All the best
Søren
More information about the Rcpp-devel
mailing list