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]]
Hi, Try this; lapply( mydata, function(x){ sum( diff( x ) ) } ) 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. > > > 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. >-- Romain Francois Independent R Consultant +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr
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
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}}
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:> 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
Dear Rafael, Perhaps: sum(diff(x)) where x is your vector. To apply above to your data set (by rows), you could use apply(mydata,1,function(x) sum(diff(x))) See ?diff, ?sum and ?apply for more information. HTH, Jorge On Thu, Mar 12, 2009 at 9:04 AM, Rafael Moral <rafa_moral2004@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@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. > >[[alternative HTML version deleted]]
> 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]. 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:> 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. > >