[R] help with loop

baptiste auguie ba208 at exeter.ac.uk
Thu Mar 12 14:32:40 CET 2009


On 12 Mar 2009, at 13:22, Richard.Cotton at hsl.gov.uk wrote:

>> 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.
>
> This is just sum(diff(x)), or even x[length(x)] - x[1].
>

I think rowSums gives what's intended,

rowSums(diff( matrix(c(1,2,3,4,5,6),ncol=2) ))

(but i'd give heads or tails as to whether this was an  
oversimplification of the real problem )


baptiste


> _________________________
> 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