1

Possible Duplicate:
How to remove rows of a matrix by row name, rather than numerical index?
removing elements in one vector from another in R

I have two vectors:

a<-c(1,2,3,4,5,6,7,8)
b<-c(7,3,6,4,8,1)

I would like to select those elements of a which are not in b

I tried subset(a, a!=b) but I get the warning:

longer object length is not a multiple of shorter object length
Community
  • 1
  • 1
user1723765
  • 6,179
  • 18
  • 57
  • 85

2 Answers2

7

Try setdiff for vectors:

R> setdiff(a,b)
[1] 2 5
rcs
  • 67,191
  • 22
  • 172
  • 153
3

Try this:

a[!(a%in%b)]

Look at ?"%in%".

Stephan Kolassa
  • 7,953
  • 2
  • 28
  • 48