Skip to content

help with loop

10 messages · Rafael Moral, Romain Francois, Nutter, Benjamin +5 more

#
Hi,

Try this;

lapply( mydata, function(x){
    sum( diff( x ) )
} )

Romain
Rafael Moral wrote:

  
    
#
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:

  
    
#
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



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

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}}
#
is your data a data frame or a matrix?  do you want to compute the
differences columnwise, i.e., for each column independently?  consider
this example:

    # generate and display dummy data
    (d = as.data.frame(replicate(3, sample(5))))

    # compute successive differences columnwise
    as.data.frame(apply(d, 2, diff))
    apply(as.matrix(d), 2, diff)

see ?apply and ?diff for details.  note that apply will return a matrix
both when given a data frame and when given a matrix.

vQ
Rafael Moral wrote:
#
This is just sum(diff(x)), or even x[length(x)] - x[1].

Regards,
Richie.

Mathematical Sciences Unit
HSL


------------------------------------------------------------------------
ATTENTION:

This message contains privileged and confidential inform...{{dropped:20}}
#
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:
#
On 12 Mar 2009, at 13:22, Richard.Cotton at hsl.gov.uk wrote:

            
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
#
Wacek Kusnierczyk wrote:
haven't noticed the sum part;  you can apply colSums to the above
resulting data frame or matrix, e.g.:

    colSums(apply(d, 2, diff))

vQ