[R] help with loop

Romain Francois romain.francois at dbmail.com
Thu Mar 12 14:13:48 CET 2009


Well actually, what about that (Assuming mydata is a data frame)

tail( mydata, 1 ) - head( mydata, 1)

since:

(the second - the first) + (the third - the second) + (the fourth - the third) = the last - the first

Romain

Rafael Moral 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.
>
>   


-- 
Romain Francois
Independent R Consultant
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr




More information about the R-help mailing list