# [R] Efficient method: Equality of all elements of two vectors

Ista Zahn istazahn at gmail.com
Sun Oct 14 19:30:20 CEST 2012

```Hi,

On Sun, Oct 14, 2012 at 11:23 AM, Shant Ch <sha1one at yahoo.com> wrote:
> n=10
> x1<-(1:n)/n
> y1<-rnorm(n,x1^2,1)
> m=20
> x2<-(1:m)/m
>
> The value of y2 will be rnorm (m, x2^2,1) if none of the elements of x2 is same as x1, but for every same elements in x1 and x2, the value of y2 will be same as y1. I know the following is correct, but for large vectors, this won't work efficiently as this is taking longer time. Can you please let me know how to write the following more efficiently.

Sounds like
y2 <- rnorm (m, x2^2,1)
y2 <- ifelse(x2==x1, y1, y2)

Best,
Ista

> -------------------
> y2<-rep(0,m)
> for(i in 1:m)
> {
>   for (j in 1:n)
>   {
>     if(x2[i]==x1[j]
>     {
>        y2[i]=y1[j]
>     }
>     else
>     {
>     y2[i]=rnorm(1,x2[i]^2,1);
>   }
> }
>         [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help