Create List of Vectors Using Rcpp Package in R (Example)

 

In the following, we show you how to do create a list of vectors with the Rcpp package in R.

The structure is as follows:

Let’s list some vectors!

 

Create Example Vectors

First, we need to load the Rcpp package to be able to use C++ code in R.

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

We create three example vectors x, y, and z, which we want to combine to a list.

x <- 1:5                    # numeric
y <- LETTERS[1:10]          # string
z <- c(TRUE, FALSE, FALSE)  # logical

You probably know from your experience with R that we can create unnamed lists and named lists. We show you an example of an unnamed and a named list of vectors x, y, and z in R below. With the example, you can also see that a list can take elements of different forms, types, and sizes.

list(x, y, z) # creates a list without specific names for the list elements
# [[1]]
# [1] 1 2 3 4 5
# 
# [[2]]
# [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
# 
# [[3]]
# [1]  TRUE FALSE FALSE
 
list("a" = x, "b" = y, "c" = z) # creates a list where we specifically name the elements
# $a
# [1] 1 2 3 4 5
# 
# $b
# [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
# 
# $c
# [1]  TRUE FALSE FALSE

Example 1: Unnamed List of Vectors in Rcpp

Like we did in R before, we can create an unnamed list of the three vectors in Rcpp with the function below.

cppFunction(' 
Rcpp::List f_vec_list( Rcpp::NumericVector x, 
                       Rcpp::StringVector  y, 
                       Rcpp::LogicalVector z) { 
 
   Rcpp::List res = Rcpp::List::create(x, y, z);
   return res;
}
')
f_vec_list(x, y, z)
# [[1]]
# [1] 1 2 3 4 5
# 
# [[2]]
# [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
# 
# [[3]]
# [1]  TRUE FALSE FALSE

 

Example 2: Named List of Vectors in Rcpp

Alternatively, we can also create a Rcpp list where we explicitly name the list elements “a”, “b”, and “c”, as shown in the function below.

cppFunction(' 
Rcpp::List f_vec_named_list( Rcpp::NumericVector x, 
                       Rcpp::StringVector  y, 
                       Rcpp::LogicalVector z) { 
 
   Rcpp::List res = Rcpp::List::create(Rcpp::Named("a") = x,
                          Rcpp::Named("b") = y,
                          Rcpp::Named("c") = z);
   return res;
}
')
f_vec_named_list(x, y, z)
# $a
# [1] 1 2 3 4 5
# 
# $b
# [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
# 
# $c
# [1]  TRUE FALSE FALSE

 

Video & Further Resources

For more information on Rcpp, we recommend you to take a look at the official Rcpp homepage here. Furthermore, on the CRAN page of Rcpp, which you can find here, you can find some useful additional references.

On https://statisticsglobe.com/, we have many more blog posts which you might want to look at:

 

We showed you how to create a list of vectors in Rcpp. If you have questions or comments, get in contact with us using the comment 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 further information about her academic background and the other articles she has written for Statistics Globe.

 

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.

The maximum upload file size: 2 MB. You can upload: image. Drop file here

Top