albeit2
albeit2

Reputation: 93

Making an new array only out of the values that are the same in two other arrays - R

Say you have the following two arrays:

Array1 = c(1,  2,  6,  7,  9, 10, 11, 12, 13, 14, 15, 18, 19)
Array2 = c(1, 3, 5,  9, 11, 14, 18)

Now say you want to make a new array which only holds the values that are the same between Array1 and Array2:

NewArray = c(1, 9, 11, 14, 18)

How does one do this without having to manually scan each Array1 and Array2 to construct NewArray?

Upvotes: 0

Views: 24

Answers (1)

D3C34C34D
D3C34C34D

Reputation: 417

You're looking for intersect:

Array1 = c(1,  2,  6,  7,  9, 10, 11, 12, 13, 14, 15, 18, 19)
Array2 = c(1, 3, 5,  9, 11, 14, 18)
NewArray = intersect(Array1, Array2)

Upvotes: 1

Related Questions