Test for Equality of All Vector Elements in R (2 Examples)
In this R tutorial you’ll learn how to check whether all elements of a vector object are the same.
The content of the post is structured as follows:
Let’s dive into it.
Construction of Example Data
I use the data below as basement for this R tutorial:
vec1 <- 1:5 # Create first example vector vec1 # Print first example vector # [1] 1 2 3 4 5
The previous output of the RStudio console shows that our first example vector contains numeric values ranging from 1 to 5.
For comparison, let’s create a second vector:
vec2 <- rep(2, 5) # Create second example vector vec2 # Print second example vector # [1] 2 2 2 2 2
Our second vector contains the value 2 five times (i.e. all elements are equal).
Let’s check for equality using some code in R…
Example 1: Check Whether All Vector Elements are the Same Using var() Function
In Example 1, I’ll explain how to apply the var() function and the == operator to test for equality of all vector elements in R.
Let’s apply the var function to the first vector…
var(vec1) == 0 # Apply var function to first vector # [1] FALSE
…and to the second vector:
var(vec2) == 0 # Apply var function to second vector # [1] TRUE
As you can see, the first R syntax returned FALSE, i.e. the elements of vec1 are not equal to each other, and the second R syntax returned TRUE, i.e. the elements of vec2 are all the same.
The logic behind the previously used R code is that the variance of a vector containing only the same value is equal to zero. We check that by using the == operator.
Example 2: Check Whether All Vector Elements are the Same Using length() & unique() Functions
In Example 2, I’ll explain how to use the length() and unique() functions to test if all elements in our vectors are the same.
Have a look at the following R code. First, we apply our R syntax to the first vector…
length(unique(vec1)) == 1 # Apply length & unique to first vector # [1] FALSE
…and then to the second vector:
length(unique(vec2)) == 1 # Apply length & unique to second vector # [1] TRUE
The previous R syntax confirms our result of Example 1. Whether you want to use the var function (as in Example 1) or the length and unique functions (as shown in Example 2) is a matter of taste.
The logic behind the R code used in this example is that there will be only a single unique value in case all elements are the same, i.e. the length of the unique values will be equal to 1.
Video, Further Resources & Summary
Do you need further info on the contents of the present article? Then I recommend having a look at the following video which I have published on my YouTube channel. I show the contents of this page in the video.
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.
Furthermore, you could read the related tutorials of this website.
- Get Frequency of Elements with Certain Value in Vector (2 R Examples)
- Randomize Vector in R
- Find Unique Combinations of All Elements from Two Vectors
- Test if Vector Contains Given Element
- R Programming Overview
In summary: You have learned in this article how to evaluate if all vector elements are equal in the R programming language. In case you have any further questions, tell me about it in the comments.
Statistics Globe Newsletter