Skip to content
Back to formatted view

Raw Message

Message-ID: <971536df0903120626j26874d4cva5c06323b2c178da@mail.gmail.com>
Date: 2009-03-12T13:26:20Z
From: Gabor Grothendieck
Subject: help with loop
In-Reply-To: <648801.99056.qm@web31605.mail.mud.yahoo.com>

This is a telescoping sum that can be calculated analytically as:

(a[2] - a[1]) + ... + (a[n] - a[n-1]) = a[n] - a[1]



On Thu, Mar 12, 2009 at 9:04 AM, Rafael Moral
<rafa_moral2004 at yahoo.com.br> wrote:
> Dear?useRs,
> I'm trying to write a loop to?sum my data in the following way:
> (the second?- the first) + (the third - the second) + (the fourth - the third) + ...
> for each column.
>
> So, I wrote something like this:
>
> ? c <- list()
> ? for(i in 1:ncol(mydata)) {
> ? for(j in 2:nrow(mydata)) {
> ? c[[i]] <- sum(yc[j,i] - yc[(j-1),i])
> ? }}}
>
> As for the columns it works pretty fine, but it only returns the last subtraction, however, I need the sum of all subtractions.
>
> Any ideas?
>
> Regards,
> Rafael.
>
>
> ? ? ?Veja quais s?o os assuntos do momento no Yahoo! +Buscados
>
> ? ? ? ?[[alternative HTML version deleted]]
>
>
> ______________________________________________
> 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.
>
>