[R] how to know whether Vector B is a subset of Vector A
P.Dalgaard at biostat.ku.dk
Fri Oct 12 14:42:11 CEST 2007
> I'm quite fresh to R, and a layman of English as well. I hope I can make you
> Now I have two vectors A and B. Is there any quick way to know whether B is
> a subset of A? and If B is a subset of A, can I know easily which elements
> in A (the index of A) equals to B's elements accordingly?
> For example,
> Question 1: we know b is a subset of a, but how does R know that?
> Question 2: since we know b is a subset of a, then which a's elements equals
> to b? I do it like this:
>> for (i in 1:length(b)) test[i]=which(a==b[i])
> Is there any easier way to know these indexes?
> Thanks a lot for you helps in advance
> a %in% b
 FALSE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE FALSE FALSE FALSE
 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> which(a %in% b)
 2 3 4 5 7 8 9
O__ ---- Peter Dalgaard Øster Farimagsgade 5, Entr.B
c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help