# [R] vector help

David Winsemius dwinsemius at comcast.net
Thu Dec 10 22:12:40 CET 2009

```On Dec 10, 2009, at 3:47 PM, Steve Lianoglou wrote:

> Hi Ramya,
>
> On Dec 10, 2009, at 3:29 PM, David Winsemius wrote:
>
>>
>> On Dec 10, 2009, at 2:55 PM, Ramya wrote:
>>
>>>
>>> I have tow vectors one is the subset of another
>>>
>>> x is a subset of X Both are vectors with n elements
>>>
>>> X[X %in% x] would give me x again rite because it is a subset but
>>> i want all
>>> those are not in x from X.
>>>
>>> X[which(X != x)] should this do that
>
> One way to increase your proficiency in R is to break out your
> statements so that each line does 1 thing.
>
> You should look at the object that `X != x` returns to you. [Shuffle
> the elements in X and x and then see how that changes for extra
> credit]
>
> Then look at what `which(X != x)` gives you.
>
> Doing that, you'd see why what you tried originally didn't work.
>
>>
>> Perhaps you think R _should_ read your mind, but we are not there
>>
>> X[!(X %in% x)]
>
> There are also set-like functions in R, which might fit your brain-
> way of thinking:
>
> Get elements that are in both x and X:
> intersect(X,x)
>
> Get elements in X that are not in x
> setdiff(X,x)

Yes there are, and beware that the setdiff function is an asymmetric
version rather than what I expected,  which was the complement of the
intersection;

> ?setdiff
> X
[1] "H" "J" "E" "A" "F" "D" "B"
> x
[1] "H" "B" "F" "A" "Z"
> setdiff(union(X,x), intersect(X,x))  # which is what I expected
[1] "J" "E" "D" "Z"
> setdiff(X,x)   # what you get
[1] "J" "E" "D"
>
>
> --
> Steve Lianoglou
> Graduate Student: Computational Systems Biology
>  |  Memorial Sloan-Kettering Cancer Center
>  |  Weill Medical College of Cornell University
> Contact Info: http://cbio.mskcc.org/~lianos/contact
>

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

```