Skip to content
Back to formatted view

Raw Message

Message-ID: <07773E68C32A644CA47651463E79E5C20247725F@CCHSCLEXMB68.cc.ad.cchs.net>
Date: 2009-03-12T13:16:37Z
From: Nutter, Benjamin
Subject: help with loop
In-Reply-To: <648801.99056.qm@web31605.mail.mud.yahoo.com>

Why use a loop? Try using diff()

x <- c(4, 19, 21, 45, 50, 73, 78, 83, 87, 94)
sum(diff(x))

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Rafael Moral
Sent: Thursday, March 12, 2009 9:04 AM
To: r-help at r-project.org
Subject: [R] help with loop

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


===================================

P Please consider the environment before printing this e-mail

Cleveland Clinic is ranked one of the top hospitals
in America by U.S. News & World Report (2008).  
Visit us online at http://www.clevelandclinic.org for
a complete listing of our services, staff and
locations.


Confidentiality Note:  This message is intended for use\...{{dropped:13}}