Convert std::vector to Rcpp Matrix in R (2 Examples)

 

We show you how to build an Rcpp matrix with entries filled by a std Rcpp vector. In addition, we also show you how to do it with RcppArmadillo matrices and vectors. See our post on Rcpp and RcppArmadillo first if both expressions are new to you.

We cover the following blocks:

Let’s start.

 

Example 1: Standard Rcpp Matrix

Load the Rcpp package.

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

We create a C++ function which we call “fun_gen_mat” which generates a matrix. As input, we use a vector called “values” containing the values of the matrix and a 2-vector “dimensions” containing the number of rows and columns of the matrix. In standard Rcpp, we first define an empty matrix of the desired dimensions and then fill its entries using std::copy().

cppFunction(' 
Rcpp::NumericMatrix fun_gen_mat( Rcpp::NumericVector values, Rcpp::NumericVector dimensions ) {
 
   // Create an empty matrix with dimensions "dimensions"
   Rcpp::NumericMatrix mat(dimensions[0], dimensions[1]);
 
   // Fill matrix "mat" by values of vector "values"
   std::copy(values.begin(), values.end(), mat.begin());  
 
   return mat;
}
')

Test the function:

fun_gen_mat(values = 1:(5*2), dimensions = c(2,5))
#      [,1] [,2] [,3] [,4] [,5]
# [1,]    1    3    5    7    9
# [2,]    2    4    6    8   10

 

Example 2: RcppArmadillo Matrix

Similarly, we can create an Armadillo matrix using RcppArmadillo, see our blog port about RcppArmadillo here. As a data scientist, the rich linear algebra functionality of RcppArmadillo is very useful.

cppFunction(depends = "RcppArmadillo", ' 
arma::mat fun_gen_arma_mat( arma::vec values, arma::vec dimensions ) {
 
   // Create an empty matrix with dimensions "dimensions"
   arma::mat mat(dimensions[0], dimensions[1]);
 
   // Fill matrix "mat" by values of vector "values"
   std::copy(values.begin(), values.end(), mat.begin());  
 
   return mat;
}
')

Test the function:

fun_gen_arma_mat(values = 1:(5*2), dimensions = c(2,5))
#      [,1] [,2] [,3] [,4] [,5]
# [1,]    1    3    5    7    9
# [2,]    2    4    6    8   10

 

Video & Further Resources

We showed you two examples of how to generate Rcpp matrices from vectors. For more information on C++, you can take a look at the following YouTube video which shows a C++ introduction by Igor Bogoslavskyi, uploaded by the Cyrill Stachniss channel.

 

 

We have many more R and Python programming posts at https://statisticsglobe.com/ which can be of interest to you:

 

This post showed you different ways of constructing a Rcpp matrix from a std::vector in R. We would love to hear your comments and questions 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. Required fields are marked *

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

Top