[Rcpp-devel] Rcpp version of c( )

Søren Højsgaard sorenh at math.aau.dk
Tue Jan 21 23:02:56 CET 2014

Dear all,

I have made the following primitive "concatenate" function, because I couldn't find one in Rcpp:

template <const int RTYPE>
Vector<RTYPE> do_conc_(Vector<RTYPE> x, Vector<RTYPE> y){
  int nx=x.size(), n=x.size()+y.size(),i,j;
  Vector<RTYPE> out=no_init(n);
  for (i=0; i<nx; ++i){ out[ i ] = x[ i ];}
  for (j=i, i=0; j<n; ++j, ++i){ out[ j ] = y[i] ;}
  return out;

// [[Rcpp::export]]
SEXP conc( SEXP& XX_, SEXP& YY_){
  int type = TYPEOF(XX_) ;
  switch( type ){
  case INTSXP  : return do_conc_<INTSXP> ( XX_, YY_ ) ;
  case REALSXP : return do_conc_<REALSXP>( XX_, YY_ ) ;
  case STRSXP  : return do_conc_<STRSXP> ( XX_, YY_ ) ;
  case VECSXP  : return do_conc_<VECSXP> ( XX_, YY_ ) ;
  return R_NilValue ;

As you can see it assumes that the two inputs XX_ and YY_ are of the same type, and it fails to copy names to the output. If I have missed any such functionality in Rcpp then I would be happy to know...


More information about the Rcpp-devel mailing list