[R] how to compare 2 numeric vectors

jim holtman jholtman at gmail.com
Mon Sep 17 21:54:28 CEST 2007


Here is one way of doing it:

> v1
 [1]  1  1 NA NA  1 NA NA NA NA  1  1  1 NA  1 NA  1 NA NA  1 NA
> v2
 [1] NA  1 NA  1  1  1  1  1 NA  1  1 NA  1  1 NA NA NA  1 NA  1
> which(!(is.na(v1) | is.na(v2)))
[1]  2  5 10 11 14
>


On 9/17/07, dxc13 <dxc13 at health.state.ny.us> wrote:
>
> useR's
>
> I am trying to compare two vectors that have the same length.  More
> specifically, I am interested in comparing the corresponding positions of
> each element in the vector.
>
> Consider these two vectors of length 20:
>
> v1 <-  2    2    4    NA  NA  NA  10  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
> NA  NA  NA
> v2 <-  NA  4  NA    NA  NA  NA  10  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
> NA  100 NA
>
> I am only interested in extracting the position number where both vectors
> are not equal to NA at the same position.
> For example, the second element of both vectors is not equal to NA, and thus
> I want to return '2'; similary, I also would want to have '7' returned
> because the seventh element of both arrays is not equal to NA.
>
> In general, I would like to find a way to do this for any number of given
> positions between the two vectors that satisfy this.  If no positions
> satisfy this, then return '0'
>
> I would like to store the results as an object.
>
> If anyone has any ideas, I would greatly appreciate it. Thanks!
>
> dxc13
> --
> View this message in context: http://www.nabble.com/how-to-compare-2-numeric-vectors-tf4469582.html#a12743842
> 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
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list