Skip to content

[Rcpp-devel] Concatenating NumericVectors

3 messages · Martin Oberhuber, Dirk Eddelbuettel

#
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.

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 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.

Thanks,

Martin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20120821/e73c24ae/attachment.html>
#
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
#
On 21 August 2012 at 21:08, Dirk Eddelbuettel wrote:
| On 21 August 2012 at 15:38, Martin Oberhuber wrote:
| | 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.

Sorry, I mean to say: "Compute n = n_a + n_b, _reserve_ a new vector of size n, ..."

Dirk