find common values between vectors in r

To find the common values between two or more vectors in R, we can use the intersect() function. This function takes two or more vectors as arguments and returns a vector containing the values that are common to all of the input vectors.

Here's an example that demonstrates the use of intersect() to find the common values between two vectors:

main.r
vec1 <- c(1, 2, 3, 4, 5)
vec2 <- c(3, 4, 5, 6, 7)

common_values <- intersect(vec1, vec2)

print(common_values)
112 chars
7 lines

Output:

main.r
[1] 3 4 5
10 chars
2 lines

In this example, vec1 and vec2 have three common values (3, 4, and 5). We use intersect() to find those common values and store the result in the common_values variable. Finally, we print the contents of common_values to the console.

gistlibby LogSnag