Creating vectors
1. Creating vectors
You know the most important things you can do with a Rcpp vector, namely get its number of elements with the "size()" method and extract the value at offset i with the square brackets. Let's now review some ways for you to create vectors.2. Get a vector from the R side
Exported Rcpp functions are meant to be called from R. You can use Rcpp vector classes as parameters to these functions. In the function extract, the parameter x is declared as a numeric vector, therefore in the body of the function, you have the guarantee that x is indeed a numeric vector.3. NumericVectors
Rcpp makes this possible by doing one of the following things: - If x is already a numeric vector, everything is fine, it is just handed over to the NumericVector class. - If x is not a numeric vector but can be coerced to one, it is, in that case, Rcpp creates a new numeric vector and copies the data into it, so that you have the guarantee that you are manipulating a numeric vector.4. NumericVectors
- If x cannot be coerced to a numeric vector, for example, if it is a vector of strings, Rcpp raises an exception.5. Create a vector of a given size
Sometimes, you want to create a vector directly in C++. For this, you can use the dedicated constructor for the vector class. For example, if you want to create a numeric vector of n elements, you use the notation "NumericVector" followed by the name of the vector you wish to create, then followed by the number of elements between parentheses. Vectors are initialized with default values in all their elements, for numeric and integer vectors, they are initialized with 0. String vectors are initialized to vectors of empty strings.6. Constructor variants
The previous code used the simple constructor. Rcpp vectors have several other constructors. If you want to create a vector of a given size with all the values set to something other than 0, you can use a constructor with two arguments. The first argument gives the size, and the second the default value.7. Given set of values
Sometimes you want to create a vector from a set of values, just like you would do with the C function in R. For this, you can use the static method "create()". A static method is a method that is related to a class but does not involve particular instances of the class. Therefore you cannot use the dot notation to invoke static methods. In C++, invoking a class's static method is done with the colon colon operator. To create a new numeric vector with values 1, 2 and 3, you then use the notation "NumericVector::create", then you enumerate the values 1, 2, 3 separated by commas inside round brackets.8. Given set of values with names
In this example, we create the named vector x with values 1, 2 and 3 and names a, b, and c. To give a name, you can use an underscore, then the name in a string inside square brackets, followed by the equal sign and then the value. As you can see with the creation of the vector y, you can skip naming some elements.9. Vector cloning
By default, in Rcpp when you change the values of a vector inside a function, you change the vector that was passed in. If instead of that, you want a vector that contains the same set of values but independent from the original vector, you can use the clone function to trigger a deep copy.10. Let's practice!
Now it's your turn.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.