Dear R experts Please do you have any idea about how this summation can be written in R(the equation can be viewed in the following link): http://s16.postimg.org/or2km30ph/equation.jpg I've tried out out this code but it gave me error for writing brackets in function of summation:>>>x=matrix(c(6,2,1),3,1) for (l in 1:3){ sum(sapply(1:3, function(j[l]){if(l>1){sum(sapply(1:j[l-1], function(j[l]){x[j[l]]*(j[l]<j[l-1])}))}}))}>>>Thank you
I cannot understand that summation at all, much less translate it to R. Do you have an original citation for this thing? As for putting subscripts in parameter lists, that is not going to happen. You have to accept that the code that calls your function needs to do any necessary subscripting before it gives that piece to your function. Keep in mind that apply functions do this by their nature without the mess of specifying it yourself. If you know that the automatic subscripting that sapply does is not going to get the result you want then don't use that function. -- Sent from my phone. Please excuse my brevity. On November 30, 2015 1:20:34 PM PST, Sherouk Moawad via R-help <r-help at r-project.org> wrote:>Dear R experts >Please do you have any idea about how this summation can be written in >R(the equation can be viewed in the following link): >http://s16.postimg.org/or2km30ph/equation.jpg > >I've tried out out this code but it gave me error for writing brackets >in function of summation: > >>>> >x=matrix(c(6,2,1),3,1) > >for (l in 1:3){ >sum(sapply(1:3, function(j[l]){if(l>1){sum(sapply(1:j[l-1], >function(j[l]){x[j[l]]*(j[l]<j[l-1])}))}}))}>>>Thank you > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.[[alternative HTML version deleted]]
David Winsemius
2015-Nov-30 22:27 UTC
[R] summation equation whose numerator has subscript
> On Nov 30, 2015, at 1:20 PM, Sherouk Moawad via R-help <r-help at r-project.org> wrote: > > Dear R experts > Please do you have any idea about how this summation can be written in R(the equation can be viewed in the following link): > http://s16.postimg.org/or2km30ph/equation.jpgCan you explain in natural language the goals of this expression. It makes little sense to me to start with an index of j_sub_l = 0 and to then iterate to up to j_sub_(l-1) -1 . How can there be a value for j_sub(l-1) with a starting point of zero. The notation saying to do something for l = 2:n is not helpful since values of ?l? doesn?t really appear in the looped expression (noting that j_sub_l starts at 0, so it's not being determined by ?l". I believe the confused notation was the cause of this question being closed after it appeared last week on SO: http://stackoverflow.com/questions/33882285/summation-equation-whose-numerator-has-subscript And what intent is meant for the indices of the outer summation? The expression j_sub_1 = 0 seems to have no corresponding reference point inside the looped expression. So you would simply be summing the same value N times, but since N is not defined we cannot write any code.> > I've tried out out this code but it gave me error for writing brackets in function of summation:You should _always_, _always_, _always_ post the entire results of an error. We have no way of seeing your console. Error messages are usually informative.> >>>> > x=matrix(c(6,2,1),3,1) > > for (l in 1:3){ > sum(sapply(1:3, function(j[l]){if(l>1){sum(sapply(1:j[l-1], function(j[l]){x[j[l]]*(j[l]<j[l-1])}))}}))}You have three nested loops in the code above, but at least it appears you do understand that R is a 1-based language. But since the image-expression goes from 0 to some cryptic value (minus one) then the R version ought to go from one to "one more? than that expression.> >>>Thank you > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.David Winsemius Alameda, CA, USA