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.
-------------------
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@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.

Reply via email to