[R] summation coding

Rainer Schuermann rainer.schuermann at gmx.net
Fri Oct 19 09:35:24 CEST 2012


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:
> 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.
> 
> ______________________________________________
> 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.




More information about the R-help mailing list