[R] Basic vector operations was: Function to approximate complex integral

Marc Schwartz (via MN) mschwartz at mn.rr.com
Wed Apr 19 21:46:15 CEST 2006


On Wed, 2006-04-19 at 15:25 -0400, Doran, Harold wrote:
> Dear List
> 
> I apologize for the multiple postings. After being in the weeds on this
> problem for a while I think my original post may have been a little
> cryptic. I think I can be clearer. Essentially, I need the following
> 
> a <- c(2,3)
> b <- c(4,5,6)
> 
> (2*4) + (2*5) + (2*6) + (3*4) + (3*5) +(3*6)
> 
> But I do not know of a built in function that would do this. Any
> suggestions?

<SNIP>

Unless I am missing something, how about:

> sum(a %x% b)
[1] 75

See ?"%x%"

You could also use outer():

> sum(outer(a, b, "*"))
[1] 75

See ?outer

HTH,

Marc Schwartz




More information about the R-help mailing list