# Test if Rcpp CharacterVector Elements are Equal in R (3 Examples)

Often with strings, we want to know whether some character/string elements of a vector are actually equal. We show you some examples for doing that with Rcpp objects in R.

The structure is as follows:

“Banana” and “Apple”, same or not, let’s check 🙂

## Example 1: Check Whether All Vector Elements Are The Same

We 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' |

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

In our first example, we create a function “fun_all_eq” which checks whether all elements of an input string vector are the same. It returns a logical expression.

cppFunction(' int f_char_cpp( Rcpp::StringVector x) { // Get the size of the input vector int n = x.size(); // Create a logical vector "vec_log" Rcpp::LogicalVector vec_log(n); // Successively for each element in x check whether it // equals the first element of x for (int i = 0; i < n; i++) { vec_log(i) = (x(i) == x(0)); } // Check whether all values of "vec_log" are true bool res = is_true ( all(vec_log) ); return res; } ') |

cppFunction(' int f_char_cpp( Rcpp::StringVector x) { // Get the size of the input vector int n = x.size(); // Create a logical vector "vec_log" Rcpp::LogicalVector vec_log(n); // Successively for each element in x check whether it // equals the first element of x for (int i = 0; i < n; i++) { vec_log(i) = (x(i) == x(0)); } // Check whether all values of "vec_log" are true bool res = is_true ( all(vec_log) ); return res; } ')

You can see that within the function we use function *all()* to check whether all elements of *vec_log* are true. The *all()* function is part of **Rcpp sugar**. It is very useful because it allows us to check a complete vector for a certain feature instead of checking for each of its elements separately. Have a look at the sugar documentation here or our blog post about sugar functions here to learn more.

We test the function for two vectors “x” and “y”. For comparison, we also added the equivalent R code.

x <- c("a", "and", "all", "a", "a") y <- c("a", "a", "a") # Test in R length(unique(x)) == 1 # [1] FALSE length(unique(y)) == 1 # [1] TRUE # Test using function "f_char_cpp" f_char_cpp(x) # [1] 0 f_char_cpp(y) # [1] 1 |

x <- c("a", "and", "all", "a", "a") y <- c("a", "a", "a") # Test in R length(unique(x)) == 1 # [1] FALSE length(unique(y)) == 1 # [1] TRUE # Test using function "f_char_cpp" f_char_cpp(x) # [1] 0 f_char_cpp(y) # [1] 1

## Example 2: Check For Each Vector Element Whether It Is Equal To A String

In this example, we want to define a function “f_char_cpp2” which checks for each element of an input vector “x” if it equals a certain input string “y”.

cppFunction(' Rcpp::LogicalVector f_char_cpp2( Rcpp::StringVector x, Rcpp::String y) { int n = x.size(); Rcpp::LogicalVector vec_log(n); for (int i = 0; i < n; i++) { vec_log(i) = (x(i) == y); } return vec_log; } ') |

cppFunction(' Rcpp::LogicalVector f_char_cpp2( Rcpp::StringVector x, Rcpp::String y) { int n = x.size(); Rcpp::LogicalVector vec_log(n); for (int i = 0; i < n; i++) { vec_log(i) = (x(i) == y); } return vec_log; } ')

Compare the code to the code of the previous example to see what we did different this time. Let us test the function. Again, we also added the equivalent R code.

x # [1] "a" "and" "all" "a" "a" # Test in R x == "a" # [1] TRUE FALSE FALSE TRUE TRUE # Test using function "f_char_cpp2" f_char_cpp2(x, "a") # [1] TRUE FALSE FALSE TRUE TRUE |

x # [1] "a" "and" "all" "a" "a" # Test in R x == "a" # [1] TRUE FALSE FALSE TRUE TRUE # Test using function "f_char_cpp2" f_char_cpp2(x, "a") # [1] TRUE FALSE FALSE TRUE TRUE

You see that function “f_char_cpp2” returns a logical vector which is of the same size as the input vector.

## Example 3: Get The Position Of Vector Elements Which Equal A String

In the previous example, the function returned a logical vector with the size of the input vector, which returned whether the corresponding element of the input vector is equal to a string. In this example, we go one step further and want to define a function “f_char_cpp3” which instead returns the positions of the elements which are equal to the string.

cppFunction(' Rcpp::IntegerVector f_char_cpp3( Rcpp::StringVector x, Rcpp::String y) { int n = x.size(); // Define a vector "vec_pos" without a specific length Rcpp::IntegerVector vec_pos; for (int i = 0; i < n; i++) { if (x(i) == y) { // The push_back() function is used to fill vector "vec_pos" vec_pos.push_back(i); // For illustration, print out "vec_log" in each iteration Rcout << vec_pos<< std::endl; } } return vec_pos; } ') |

cppFunction(' Rcpp::IntegerVector f_char_cpp3( Rcpp::StringVector x, Rcpp::String y) { int n = x.size(); // Define a vector "vec_pos" without a specific length Rcpp::IntegerVector vec_pos; for (int i = 0; i < n; i++) { if (x(i) == y) { // The push_back() function is used to fill vector "vec_pos" vec_pos.push_back(i); // For illustration, print out "vec_log" in each iteration Rcout << vec_pos<< std::endl; } } return vec_pos; } ')

In the code, we use function *.push_back()*, you can see another example of the function in the Rcpp introduction here. We test the function to see what *.push_back()* actually does.

x # [1] "a" "and" "all" "a" "a" # Test in R which(x == "a") # [1] 1 4 5 # Test using function "f_char_cpp3" f_char_cpp3(x, "a") # 0 # 0 3 # 0 3 4 # [1] 0 3 4 |

x # [1] "a" "and" "all" "a" "a" # Test in R which(x == "a") # [1] 1 4 5 # Test using function "f_char_cpp3" f_char_cpp3(x, "a") # 0 # 0 3 # 0 3 4 # [1] 0 3 4

As we print out the intermediate results of vector “vec_pos”, we can see that function *.push_back()* successively adds additional elements to a vector.

## Video & Further Resources

You not only want to learn about strings in Rcpp, but also in standard R? Then check out the Statistics Globe video below where you see how to extract certain characters from a string.

**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.

For more posts on R on Statistics Globe, check the following links:

- Create Data Frame & Matrix Using Rcpp Package in R (4 Examples)
- Matrix Multiplication Using Rcpp Package in R (Example)
- Subset Rcpp Matrix by Logical Condition in R (4 Examples)
- Learn R Programming (Tutorial & Examples)

We showed you different ways of checking whether the elements of a string vector are equal to each other or equal to a specific string in Rcpp. When you have comments or questions, please let us know 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.

### Statistics Globe Newsletter