Convert Vector from RcppArmadillo to Rcpp & Vice Versa in R (2 Examples)

 

As R users, we use the package Rcpp to make our R code much more efficient by integrating C++ code. We can define objects in the C++ code as standard Rcpp or RcppArmadillo objects.

In this post we will show you how to convert a vector from RcppArmadillo to standard Rcpp. And we also show you the opposite way: How to convert a standard Rcpp vector to an Armadillo vector.

Take a look at our blog posts about Rcpp and RcppArmadillo first if both terms are new to you.

This post has the following structure:

Let’s do some vector conversion.

 

Example 1: Convert Vector from RcppArmadillo to Rcpp

Load the Rcpp package.

if (!require('Rcpp', quietly = TRUE)) { install.packages('Rcpp') } 
library('Rcpp') # Load package 'Rcpp'

As an example, we create a function “f_example” which interprets its input vector as an Armadillo vector and transforms it into an Rcpp vector.

cppFunction(depends = "RcppArmadillo", ' 
Rcpp::NumericVector f_example( arma::vec vec_arma ) {
 
   // Transform Armadillo vector "vec_arma" into a standard Rcpp vector
   // Here are 2 possibilities to do that
   Rcpp::NumericVector vec_Rcpp   = as<NumericVector>(wrap(vec_arma));
   Rcpp::NumericVector vec_Rcpp_2 = NumericVector(vec_arma.begin(), vec_arma.end());
 
   // Take a look at the the different vectors
   Rcout << "vec_arma:\\n"   << vec_arma   << std::endl;
   Rcout << "vec_Rcpp:\\n"   << vec_Rcpp   << "\\n" << std::endl;
   Rcout << "vec_Rcpp_2:\\n" << vec_Rcpp_2 << "\\n" << std::endl;
 
   return vec_Rcpp_2;
}
')

We use Rcout to print certain objects in the console. It is pretty useful for writing C++ code and taking a look at some intermediate results. Both lines of code, as(wrap(vec_arma)) and NumericVector(vec_arma.begin(), vec_arma.end()) transform the Armadillo vector into an Rcpp vector. We can run an example to see the different versions of the vector.

f_example(1:5)
 
# vec_arma:
#   1.0000
#   2.0000
#   3.0000
#   4.0000
#   5.0000
# 
# vec_Rcpp:
#   1 2 3 4 5
# 
# vec_Rcpp_2:
#   1 2 3 4 5
# 
# [1] 1 2 3 4 5

 

Example 2: Convert Vector from Rcpp to RcppArmadillo

Now, we do it the other way around. We create a function “f_example_2” which interprets its input vector as an Rcpp vector and transforms it into an Armadillo vector.

cppFunction(depends = "RcppArmadillo", ' 
arma::vec f_example_2( Rcpp::NumericVector vec_rcpp ) {
 
   // Transform Rcpp vector "vec_rcpp" into an Armadillo vector
   arma::vec vec_arma = as<arma::vec>(wrap(vec_rcpp));
 
   // Take a look at the the different vectors
   Rcout << "vec_rcpp:\\n"   << vec_rcpp   << "\\n" << std::endl;
   Rcout << "vec_arma:\\n"   << vec_arma   << "\\n" << std::endl;
 
   return vec_arma;
}
')

Take a look at the function for example values 1 to 5.

f_example_2(1:5)
 
# vec_rcpp:
#   1 2 3 4 5
# 
# vec_arma:
#   1.0000
#   2.0000
#   3.0000
#   4.0000
#   5.0000
# 
# 
# [,1]
# [1,]    1
# [2,]    2
# [3,]    3
# [4,]    4
# [5,]    5

 
You see that in RcppArmadillo, vectors are interpreted as column vectors.

Video & Further Resources

For more information, we recommend you to take a look at the CRAN information on the Rcpp and RcppArmadillo package. Furthermore, the Rcpp homepage and the Armadillo homepage are very useful.

If you want to learn more, you might want to take a look at the following video about templates from CppCon 2021, uploaded by the CppCon channel.

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

We have more videos on https://statisticsglobe.com/ which you might want to take a look at:

 

We showed you how to convert Rcpp vectors into RcppArmadillo vectors and vice versa. For questions or comments, please use the section below.

 

Anna-Lena Wölwer Survey Statistician & R Programmer

This page was created in collaboration with Anna-Lena Wölwer. Have a look at Anna-Lena’s author page to get more information about her academic background and the other articles she has written for Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top