# Construct 3D Array in Rcpp Package in R (2 Examples)

We show you how to construct 3D arrays in the Rcpp package in the R programming language.

The structure is as follows.

Let’s get started!

## Example 1: Create 3D Array (Cube) With Random Uniformly Distributed Values

For this task, we use the additional RcppArmadillo package, a particular useful package for linear algebra. At this website, you find the documentation of all the different classes and functions of RcppArmadillo.

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

First, let’s create a function cpp_construct_cube_randu() which constructs a 3D array of given dimensions. 3D arrays are like matrices with an additional dimension and called “cubes” in RcppArmadillo. The order of the dimensions in arma::cube is: Slices, rows, and columns. The argument arma::fill:randu fills an object with random uniformly distributed values.

```cppFunction(depends = "RcppArmadillo", ' arma::cube cpp_construct_cube_randu(arma::vec dimensions) { arma::cube Cube1(dimensions, dimensions, dimensions, arma::fill::randu); return Cube1; } ')```

Try out the function:

```dimensions = c(2,3,2) set.seed(7) cpp_construct_cube_randu(dimensions)   # , , 1 # # [,1] [,2] [,3] # [1,] 0.9889093 0.11569778 0.2437494 # [2,] 0.3977455 0.06974868 0.7920104 # # , , 2 # # [,1] [,2] [,3] # [1,] 0.3400624 0.1658555 0.1717481 # [2,] 0.9720625 0.4591037 0.2314771```

## Example 2: Create 3D Array (Cube) With Vector Values

Now we want to create a cube and fill it with the values of a vector x.

```cppFunction(depends = "RcppArmadillo", ' arma::cube cpp_construct_cube(arma::vec x, arma::vec dimensions) {   // Initialize empty cube (Order: slices, columns, rows) arma::cube Cube1(dimensions, dimensions, dimensions);   // Fill cube by values of vector x std::copy(x.begin(), x.end(), Cube1.begin());   return Cube1; } ')```

For the use of std::copy(), you can check out this page. We execute the function.

```dimensions = c(2,3,2) x = 1:prod(dimensions) cpp_construct_cube(x, dimensions) # , , 1 # # [,1] [,2] [,3] # [1,] 1 3 5 # [2,] 2 4 6 # # , , 2 # # [,1] [,2] [,3] # [1,] 7 9 11 # [2,] 8 10 12```

With the sequence of 1 to 24 you can see which values of x is filled in at which spot of the cube. Alternatively, in R we would do the same using the following line of code:

```array(x, dim = dimensions) # , , 1 # # [,1] [,2] [,3] # [1,] 1 3 5 # [2,] 2 4 6 # # , , 2 # # [,1] [,2] [,3] # [1,] 7 9 11 # [2,] 8 10 12```

## Video & Further Resources

For more information on the use of Rcpp, we recommend you to take a look at the Rcpp homepage which provides documentations and examples. Furthermore, we provide a blog post about the Rcpp package here. There, we also show why learning Rcpp can really speed up your code!

Furthermore, you might want to take a look at the following YouTube video from the NIMBioS channel, where Drew Schmidt provides an introduction to Rcpp.

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. If you accept this notice, your choice will be saved and the page will refresh.

In this post, we showed you two examples of how to construct 3D array, so called multidimensional cubes in RcppArmadillo, RcppArmadillo and Rcpp. We would like to read your feedback below.

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