How can we compare two vectors in R? Provide examples.

How can we compare two vectors in R? Provide examples.

In R, vectors can be compared using logical operators such as “==” (equal), “!=” (not equal), “>” (greater than), “<” (less than), “>=” (greater than or equal to), and “<=” (less than or equal to). These operators return a logical value of TRUE or FALSE depending on the comparison result. Additionally, the function “identical()” can be used to compare two vectors for exact equality. For example:

vec1 <- c(1, 2, 3)
vec2 <- c(1, 2, 3)

vec1 == vec2 # returns TRUE
vec1 > vec2 # returns FALSE
identical(vec1, vec2) # returns TRUE

Compare Two Vectors in R (With Examples)


You can use the following basic syntax to compare two vectors in R:

#check if two vectors are identical
identical(vector_1, vector_2)

#display items that are in both vectors
intersect(vector_1, vector_2)

#display items that are only in first vector, but not in second vector
setdiff(vector_1, vector_2)

The following examples show how to use this syntax in practice.

Example 1: Check if Two Vectors Are Identical

The following code shows how to use the identical() function to check if two vectors are identical:

#define vectors
vector_1 <- c('Andy', 'Bob', 'Carl', 'Doug')
vector_2 <- c('Bob', 'Carl', 'Doug', 'Ethan', 'Fred')

#check if two vectors are identical
identical(vector_1, vector_2)

[1] FALSE

The two vectors are not identical, so a value of FALSE is returned.

Example 2: Find Items that Exist in Both Vectors

The following code shows how to use the intersect() function to display the items that exist in both vectors:

#define vectors
vector_1 <- c('Andy', 'Bob', 'Carl', 'Doug')
vector_2 <- c('Bob', 'Carl', 'Doug', 'Ethan', 'Fred')

#display items that exist in both vectors
intersect(vector_1, vector_2)

[1] "Bob"  "Carl" "Doug"

The three items that exist in both vectors are displayed.

We can also use the length() function if we simply want to know how many items exist in both vectors:

#find how many items exist in both vectors
length(intersect(vector_1, vector_2))

[1] 3

Three items exist in both vectors.

Example 3: Find Items that Only Exist in One Vector

The following code shows how to use the setdiff() function to display the items that exist in the first vector, but not the second:

#define vectors
vector_1 <- c('Andy', 'Bob', 'Carl', 'Doug')
vector_2 <- c('Bob', 'Carl', 'Doug', 'Ethan', 'Fred')

#display items that exist in first vector, but not in second vector
setdiff(vector_1, vector_2)

[1] "Andy"

We can switch the two vectors around to identify the items that exist in the second vector, but not the first:

#define vectors
vector_1 <- c('Andy', 'Bob', 'Carl', 'Doug')
vector_2 <- c('Bob', 'Carl', 'Doug', 'Ethan', 'Fred')

#display items that exist in second vector, but not in first vector
setdiff(vector_2, vector_1)

[1] "Ethan" "Fred"

Two items exist in the second vector that do not exist in the first.

How to Compare Strings in R

Cite this article

stats writer (2024). How can we compare two vectors in R? Provide examples.. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-we-compare-two-vectors-in-r-provide-examples/

stats writer. "How can we compare two vectors in R? Provide examples.." PSYCHOLOGICAL SCALES, 3 May. 2024, https://scales.arabpsychology.com/stats/how-can-we-compare-two-vectors-in-r-provide-examples/.

stats writer. "How can we compare two vectors in R? Provide examples.." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-we-compare-two-vectors-in-r-provide-examples/.

stats writer (2024) 'How can we compare two vectors in R? Provide examples.', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-we-compare-two-vectors-in-r-provide-examples/.

[1] stats writer, "How can we compare two vectors in R? Provide examples.," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can we compare two vectors in R? Provide examples.. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top