[R] avoid a loop

KARAVASILIS GEORGE gkaravas at ee.duth.gr
Thu May 28 23:45:37 CEST 2009


Hello, R users.
I have the following code:

a=1:10
b=-3:15
n=5
x <- rep(0,n)
for (i in 1:n) x[i] <- sum( outer(a,b, function(s,t)  abs(a-b-i)==0) )

Can someone tell me if I could avoid the for command?

Thank you in advance.




More information about the R-help mailing list