Skip to content

summation coding

8 messages · djbanana, Bert Gunter, Rainer Schuermann +2 more

#
I would like to code the following in R: a1(b1+b2+b3) + a2(b1+b3+b4) +
a3(b1+b2+b4) + a4(b1+b2+b3)

or in summation notation: sum_{i=1, j\neq i}^{4} a_i * b_i

I realise this is the same as: sum_{i=1, j=1}^{4} a_i * b_i - sum_{i=j} a_i
* b_i

would appreciate some help.

Thank you.



--
View this message in context: http://r.789695.n4.nabble.com/summation-coding-tp4646678.html
Sent from the R help mailing list archive at Nabble.com.
#
Have you read the Introduction to R tutorial? You seem unaware of even
the most basic stuff, especially vectorization and subscripting.

Also, this smells like homework, and we don't do homework here.

-- Bert
On Thu, Oct 18, 2012 at 12:33 PM, djbanana <karl79264219 at gmail.com> wrote:

  
    
#
I am aware of the most basic stuff, especially vectorization and
subscripting.

I only gave a simple example with length 4. I need to do that for vectors of
length 190.

Are there any in-built commands?

Or should I write the loops myself?

Thank you.



--
View this message in context: http://r.789695.n4.nabble.com/summation-coding-tp4646678p4646705.html
Sent from the R help mailing list archive at Nabble.com.
#
Assuming that you actually mean
a1(b2+b3+b4) + a2(b1+b3+b4) + a3(b1+b2+b4) + a4(b1+b2+b3)
    ^  ^  ^

this might give you what you want:

x <- data.frame( a = sample( 1:10, 4 ), b = sample( 11:20, 4 ) )
x                                                                                                    
  a  b                                                                                                 
1 1 16                                                                                                 
2 7 15                                                                                                 
3 8 19                                                                                                 
4 4 13

for( i in 1 : length( x$a ) ) 
    x$c[i] <- x$a[i] * ( sum( x$b ) - x$b[i] ) 
x                                                                                                    
  a  b   c                                                                                             
1 1 16  47                                                                                             
2 7 15 336                                                                                             
3 8 19 352                                                                                             
4 4 13 200


Rgds,
Rainer
On Thursday 18 October 2012 12:33:39 djbanana wrote:
#
Hi,

I think I solved it myself by writing loops.

What I meant is: are there in-built functions in R that calculate the
following:

a1(b2+...+b190) + a2(b1+b3+...+b190) + ...
 
I managed to solve it, quite similar to what you just emailed.

Thanks anyway!



--
View this message in context: http://r.789695.n4.nabble.com/summation-coding-tp4646678p4646712.html
Sent from the R help mailing list archive at Nabble.com.
#
On 18-10-2012, at 21:33, djbanana wrote:

            
This is partly TeX notation not summation notation, whatever that may be.
And these "sums" make no sense: where is index j used in the first? where is it used in the second?

Solutions starting from you explicit formula, from slowest to fastest

sum( outer(a,b,"*") ) - sum(a*b)
sum(sapply(1:length(a),function(k) a[k]*sum(b[-k])))
sum(convolve(a,b)) - sum(a*b)   # sum(convolve(a,rev(b), type="o")) - sum(a*b)

Berend
#
On 19-10-2012, at 10:38, Berend Hasselman wrote:

            
And from Nabble another solution (not posted to the R-help list (yet?) ) which turns out to be the quickest (obviously)

sum(a*(sum(b)-b)) 

Berend
#
djbanana <karl79264219 at gmail.com> writes:
Following Rainer's setup:

x <- data.frame( a = sample( 1:10, 4 ), b = sample( 11:20, 4 ) )

isn't 

      prod( colSums(x) ) - x$a %*% x$b

what you are after?

HTH,

Chuck