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:
- Create Data Frame & Matrix Using Rcpp Package in R (4 Examples)
- Loop Over Rows & Columns of Rcpp Matrix in R (2 Examples)
- Introduction to the Rcpp Package in R (Examples)
- Learn R Programming (Tutorial & Examples)
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.
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.
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.
Thank you!
Welcome to the Statistics Globe newsletter. From now on, I’ll send you regular emails about statistics, data science, AI, and programming with R and Python.
I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.
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.
Thank you!
Please check your email inbox and click the confirmation link to complete your subscription. If you don’t see the email within a few minutes, please also check your spam/junk folder.







