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.
On May 28, 2009, at 5:45 PM, KARAVASILIS GEORGE wrote:> 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) )You don't seem to be doing anything with s and t? Did you mean: for (i in 1:n) x[i] <- sum( outer(a,b, function(s,t) abs(s-t-i) == 0) )> > > Can someone tell me if I could avoid the for command?Perhaps with the guess above: > x <- sapply(1:n, function(i) { sum( outer(a,b, function(s,t) {abs(s-t-i)==0 } )) } ) > x [1] 10 10 10 10 9> > > Thank you in advance.Does your code actually run in 2,9.0? (I'm still using using 2.8.1) If this isn't useful, then perhaps you should tell us what problem you are trying to solve. -- David Winsemius, MD Heritage Laboratories West Hartford, CT
Try this:> sapply(1:n, function(i) sum(abs(outer(a, b, "-")-i)==0))[1] 10 10 10 10 9 On Thu, May 28, 2009 at 5:45 PM, KARAVASILIS GEORGE <gkaravas at ee.duth.gr> wrote:> 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. > > ______________________________________________ > 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. >
Gabor Grothendieck wrote:> Try this: > >> sapply(1:n, function(i) sum(abs(outer(a, b, "-")-i)==0)) > [1] 10 10 10 10 9abs(outer(a, b, "-") - i) == 0 ==> outer(a, b, "-") == i. sum(outer(a, b, "-") == i) asks how many times i is an element of outer(a, b, "-"). This is tabulate(outer(a, b, "-"), n) I think, anyway. Martin> > > On Thu, May 28, 2009 at 5:45 PM, KARAVASILIS GEORGE <gkaravas at ee.duth.gr> wrote: >> 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. >> >> ______________________________________________ >> 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. >> > > ______________________________________________ > 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.-- Martin Morgan Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M1 B861 Phone: (206) 667-2793