[Rcpp-devel] table, rev and missing names

Romain François romain at r-enthusiasts.com
Fri Jun 6 15:55:12 CEST 2014


Hello, 

You pretty much have to do this manually. rev is part of Rcpp sugar. Defined here:
https://github.com/RcppCore/Rcpp/blob/master/inst/include/Rcpp/sugar/functions/rev.h

It does not preserve names. That’s by design. It would have been very difficult to do it automatically, i.e. what happens here: rev( x * y ), etc … it is hard to decide which names to propagate, harder to try to follow R rules about it, and even harder to implement. 

So if you want names, you have to handle them yourself. 

Romain

Le 6 juin 2014 à 12:58, Sven E. Templer <sven.templer at gmail.com> a écrit :

> Hi,
> 
> consider this code:
> 
> 
> require(Rcpp)
> require(inline)
> t <- cxxfunction(sig=c(xx="character"), plugin="Rcpp", body='
> CharacterVector x(xx);
> IntegerVector t = table(x);
> return t;')
> r <- cxxfunction(sig=c(xx="character"), plugin="Rcpp", body='
> CharacterVector x(xx);
> IntegerVector t = table(x);
> IntegerVector r = rev(t);
> return r;')
> o <- cxxfunction(sig=c(xx="character"), plugin="Rcpp", body='
> CharacterVector x(xx);
> IntegerVector t = table(x);
> CharacterVector n = t.names();
> IntegerVector r = rev(t);
> r.names() = rev(n);
> return r;')
> t(c('a','a','b',NA))
> r(c('a','a','b',NA))
> o(c('a','a','b',NA))
> 
> 
> Reversed vectors loose their name ( t() vs r() ) and have to be copied
> and reversed 'manually' ( as in o() ). Is rev() a function declared by
> Rcpp, if so can names be reserved while reverting or is there any
> other easier solution than in o() ?
> 
> Thank you!
> Sven
> _______________________________________________
> 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