[Rcpp-devel] Concatenating NumericVectors

Dirk Eddelbuettel edd at debian.org
Wed Aug 22 04:08:20 CEST 2012


Hi Martin,

On 21 August 2012 at 15:38, Martin Oberhuber wrote:
| Dear Rcpp Users,
| 
| I would like to concatenate two NumericVectors. For instance:
| 
| ...
| NumericVector a( some_STL_vector ) ;
| NumericVector b( some_STL_vector ) ;
| 
| ...  // using Rcpp sugar on a and b
| 
| a.insert( a.end(), b.begin(), b.end() );
| 
| ... // using Rcpp sugar on a
| 
| 
| 
| This would work fine if a and be were of type std::vector<double>. However,
| NumericVector does not seem to have an insert function which supports the
| structure above and therefore this isn't working.

Rcpp objects are proxy objects for R objects. And R objects do not grow
gracefully.

We do offer STL-alike functions like push_back() but also warn that they will
not be high-performance as _every_ addition will create a full copy of the
full vector. That is the price of being close to R objects.

| Any ideas on how I could concatenate the two vectors using a different
| methodology? The reason I would like to use NumericVectors here is because I am

One easy way: Compute n = n_a + n_b, insert a new vector of size n, insert a,
insert b -- and wrap all that in a little helper function.

More elegant solutions are welcome as patches :)

| using Rcpp sugar on a and b and the resulting concatenated vector and I would
| like to avoid converting them back and forth between std::vector and
| NumericVector.

For those reasons I have at times switched to using Armadillo vectors as
containers.

Hope this helps,  Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com  


More information about the Rcpp-devel mailing list