r get same elements in two vectors code example
Example 1: r find elements in common between vectors
# Basic syntax:
intersect(vector_1, vector_2) # For two vectors
Reduce(intersect, list(vector_1, vector_2, vector_3)) # For multiple vectors
# Example usage:
vector_1 = c(1,2,3,4,5)
vector_1 = c(2,4,6,8)
intersect(vector_1, vector_2)
--> 2 4
# Note, the vectors don't have to have the same length
Example 2: r find difference between two vectors
# setdiff gives the the difference of the 1st compared to the second.
setdiff(b,a)