Let’s say I have two vectors:
x <- c(1,16,20,7,2)
y <- c(1, 7, 5,2,4,16,20,10)
I want to remove elements in y that are not in x. That is, I want to remove elements 5, 4, 10 from y.
y
[1] 1 7 2 16 20
In the end, I want vectors x and y to have to same elements. Order does not matter.
My thoughts: The match function lists the indices of the where the two vectors contains a matching element but I need a function is that essentially the opposite. I need a function that displays the indices where the elements in the two vectors don’t match.
# this lists the indices in y that match the elements in x
match(x,y)
[1] 1 6 7 2 4 # these are the indices that I want; I want to remove
# the other indices from y
Does anyone know how to do this? thank you
You are after
intersectIf you want the indices for the elements of
yinx, usingwhichand%in%(%in%usesmatchinternally, so you were on the right track here)As @joran points out in the comments
intersectwill drop the duplicates, so perhaps a safe option, if you want to return true matches would be something likeYou then need to be careful about ordering with this modified function (which is avoided with
intersectas it drops duplicated elements )If you want the index of those element of y not in x, simply prefix with
!as `%in% returns a logical vectorOr if you want the elements use
setdiff