# [R] Odp: Finding the difference between two vectors

Petr PIKAL petr.pikal at precheza.cz
Tue Feb 2 17:15:59 CET 2010

```Hi

Option one

vec1<-sample(letters[1:10])
vec2<-vec1[-5]
vec2<-c(vec2,NA)
# missing position
mis<-which(!(vec1 %in% vec2))

c(vec2[1:(mis-1)], NA,vec2[mis:((length(vec2)-1))])
[1] "f" "e" "g" "d" NA  "j" "c" "i" "b" "h"

gives you desired vector. For two or more values missing it shall be
rephrased probably by rle

Option two

mis<-which(!(vec1 %in% vec2))
vec2 <- vec1
vec2 [mis] <-NA

Which could be used in case of several values missing.

But I wonder what you want to achieve. It seems to me like you want to do
something for which merge can be used.

See ?merge

Regards
Petr

r-help-bounces at r-project.org napsal dne 02.02.2010 16:34:23:

>
> Hello everyone,
> I have two vectors having only one element different:
>                            vector1         vector2
> vector1
>
>                            TWC             TWC
> TWC
>                            VFC              TWX
> NA
>                           VIA/B             VFC
> VFC
>                           WHR             VIA/B
> VIA/B
>                            WPO             WHR   --------->>>        WHR
>                            WYN             WPO
> WPO
>                            WYNN          WYN
> WYN
>                            YUM            WYNN
> WYNN
>                            NA               YUM
> YUM
> In that case, in the first vector, the value twx is missing . What I
want to
> do is doing the following avoiding loop:
> putting the NA row in front of the TWX row as this would be the row
where
> the value is missing. I don't know if there is any function doing this.
I
> thought about using a which() function to retrieve all the missing
elements
> index ( twx, 1 here) and insert them in the right index in the first
vector
> but how can I insert in a vector, is there an appropriated function?
thank
> you
>
> -----
> Anna Lippel
> --
> View this message in context:
http://n4.nabble.com/Finding-the-difference-
> between-two-vectors-tp1460020p1460020.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help