[R] how to compare 2 numeric vectors
Moshe Olshansky
m_olshansky at yahoo.com
Tue Sep 18 05:29:38 CEST 2007
Below is one way to do this:
>
v1<-c(2,2,4,NA,NA,NA,10,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA)
>
v2<-c(NA,4,NA,NA,NA,NA,10,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,100,NA)
> length(v1)
[1] 20
> length(v2)
[1] 20
> intersect(which(!is.na(v1)),which(!is.na(v2)))
[1] 2 7
>
--- 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.
>
More information about the R-help
mailing list