# [R] summation sign

arun smartpink111 at yahoo.com
Sun Oct 28 17:02:52 CET 2012

```Hi Sara,

Not very clear either.

You mentioned you want to repeat the sum 5 times.  Is it on the same x variable or different x variables?
If you want to repeat the sum 5 times on the same x variable,

x1<-c(.3,.2,.1)
fun1<-function(x,i){
res<-lapply(1:i,function(y) -sum(log(1-x^2)))
res1<-unlist(res)
res1}
fun1(x1,5)
#[1] 0.145183 0.145183 0.145183 0.145183 0.145183

But, if you want  to do the sum on different vector of same or different length

x1<-c(.3,.2,.1)
x2<-c(.5,.6,.1)
x3<-c(.1,.6,.4)
x4<-c(.2,.7.,.3)

fun1<-function(x1,...){
res1<-lapply(list(x1,...),function(x) -sum(log(1-x^2)))
res2<-unlist(res1)
res2}
fun1(x1,x2,x3,x4)
#[1] 0.1451830 0.7440195 0.6306908 0.8084772

#or
fun2<-function(x1,x2,x3,x4){
res1<-lapply(list(x1,x2,x3,x4),function(x) -sum(log(1-x^2)))
res2<-unlist(res1)
res2}
fun2(x1,x2,x3,x4)
#[1] 0.1451830 0.7440195 0.6306908 0.8084772
A.K.

----- Original Message -----
From: Sara Farooqi <sffarooqi at gmail.com>
To: arun <smartpink111 at yahoo.com>
Cc:
Sent: Sunday, October 28, 2012 2:39 AM
Subject: Re: [R] summation sign

hi Arun,
thank you for  email. No I want to repeat the sum 5 times. So i is 5
and hence there are 5 x-variables. Each of these x variables is a 3x1
vector for example.
So my question is how do I get R to repeat the sum 5 times and
recognize that each x is a 3x1 vector.
thank you!

On Sat, Oct 27, 2012 at 11:30 PM, arun <smartpink111 at yahoo.com> wrote:
> HI,
> Your question is not that clear.
> Do you want to repeat the -sum() n times and then get the grandtotal?
>  x1<-c(.3,.2,.1)
> fun1<-function(x,n){
>  res<-sum(rep(-sum(log(1-x^2)),n))
>  res}
>
>
>  fun1(x1,5)
> #[1] 0.725915
>  fun1(x1,6)
> #[1] 0.8710981
>  fun1(x1,8)
> #[1] 1.161464
> A.K.
>
>
>
>
> ----- Original Message -----
> From: sffarooqi <sffarooqi at gmail.com>
> To: r-help at r-project.org
> Cc:
> Sent: Saturday, October 27, 2012 7:40 AM
> Subject: Re: [R] summation sign
>
> thank you vm for the reply! Just to make things a little more complicated
> --does the equation hold even if the values of x are unknown? More
> specifically perhaps we can still refer to the made up example
> Sum(n equal 5)log(1-xi^2)
>
> In the problem I am working on, I am writing a modified Newton Optimisation
> code with linesearch. Therefore the values of x are not given but each x
> variable is some nx1 vector ..e.g. lets take a 3x1 vector.
> So referring to this example, where we had to sum 5 times, how could I write
> the function such that it sums 5 times, but each of the 5 x variables is
> taken to be a 3x1 vector.
>
> thank you very much again!
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/summation-sign-tp4647621p4647655.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