[R] how to know whether Vector B is a subset of Vector A
Peter Dalgaard
P.Dalgaard at biostat.ku.dk
Fri Oct 12 14:42:11 CEST 2007
Samuel wrote:
> Hi,
>
> I'm quite fresh to R, and a layman of English as well. I hope I can make you
> understood.
>
> 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,
>
>
>> a<-1:20
>> b=c(2,5,9,7,4,8,3)
>>
>>
>
> 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:
>
> test=0
>
>> for (i in 1:length(b)) test[i]=which(a==b[i])
>> test
>>
>>
>
> Is there any easier way to know these indexes?
>
> Thanks a lot for you helps in advance
>
>
> a<-1:20
> b<-c(2,5,9,7,4,8,3)
> !length(setdiff(b,a))
[1] TRUE
> a %in% b
[1] FALSE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE FALSE FALSE FALSE
[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> which(a %in% b)
[1] 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
mailing list