1. Rcpp classes and vectors
Now that you know
2. Previously on this course
how to create C++ functions and write loops, let's have a look at one of the most important features of Rcpp.
3. Vector classes
Rcpp defines a set of C++ classes that greatly simplifies manipulation and creation of R objects, particularly vectors. This slide lists all the available Rcpp vector classes.
Declaring objects as instances of these classes gives you certain guarantees about them as well as a nice and consistent interface to manipulate them.
4. Vector classes api
We will see various ways to create vectors shortly, but once you have one, the operations you typically want to perform on it are: extracting its size, that is the number of elements that the vector holds, you achieve this by calling the "size()" method on the vector object.
In C++ as in many other languages, the dot(.) is used to call a method. You invoke the "size()" method on the object x by writing "x.size()", open bracket and then close bracket because the size method does not take any extra parameters.
C++ allows the definition of square brackets operators on classes. Rcpp uses this to allow you to extract and set single values from a vector. To extract the element on index "i", you write "x" square bracket( [ ), i, then close square bracket( ] ), or x[i].
5. C++ indexing
In C++, indexing starts at 0, not 1 as it is the case in R. You can view the index as an offset to the first position, hence at the offset 0 you access the first element.
6. Insert title here...
This is the source of many errors when people start using Rcpp, and especially when you switch between R and C++ contexts.
7. The first element of a vector
As a consequence, to extract the first value of the vector x, you use "x[0]".
8. The last element of a vector
Things are even more confusing when you want to manipulate the last value of a vector. Say you have a vector x of size n, you access its last value with "x[n-1]".
Again, think of these indices as offsets, if the first element is at offset 0, and you move n steps to the right, you are one step too far.
9. Looping around a vector
The 0-based indexing is confusing at first, but it is one of those things you get used to.
However most of the time, you don't manipulate only one value, but instead, scan through an entire vector with a for-loop. Remember the for-loop starts at "i = 0" and goes as long as i is lower than n?
If you have the size n of the vector x, you can go through every value of the vector with the idiomatic for-loop we've seen in the previous chapter.
As we've seen before the for-loop is made of four parts. Let's examine together these four parts in the context of manipulating vectors.
First, you create an integer index called "i" with its initial value set to 0. The loop increment is written "i++", which means that at the end of each iteration the index i is incremented by 1. The end condition is i strictly lower than n.
Remember that the last value is at offset "n - 1", so you definitely do not want the index to go beyond n.
The notation is very idiomatic and it is the loop construct you will see most often in Rcpp.
Finally, in the body of the loop, you manipulate the current element using the square brackets.
10. Let's practice!
Time to put this into practice.