Hi all, i have the following.. a <- data.frame(data = seq(1,10)) i have indices: x <- c(1, 5, 3, 9) y <- c(2, 7, 4, 10) I want the cumsum of a[1:2], a[5:7], a[3:4]... is there an elegant way to do it without any loop? Thanks! -- View this message in context: http://www.nabble.com/cumsum-list..-tp16025202p16025202.html Sent from the R help mailing list archive at Nabble.com.
cumsum( mapply(function(i,j) sum(a$data[i:j]), x, y) ) Is this what you want? Gabor On Thu, Mar 13, 2008 at 06:02:13AM -0700, yoooooo wrote:> > Hi all, i have the following.. > > a <- data.frame(data = seq(1,10)) > > i have indices: > x <- c(1, 5, 3, 9) > y <- c(2, 7, 4, 10) > > I want the cumsum of a[1:2], a[5:7], a[3:4]... > > is there an elegant way to do it without any loop? Thanks! > > -- > View this message in context: http://www.nabble.com/cumsum-list..-tp16025202p16025202.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.-- Csardi Gabor <csardi at rmki.kfki.hu> UNIL DGM
In this case you can simply do cumsum(a[x,]+a[y,]) Julian yoooooo wrote:> Hi all, i have the following.. > > a <- data.frame(data = seq(1,10)) > > i have indices: > x <- c(1, 5, 3, 9) > y <- c(2, 7, 4, 10) > > I want the cumsum of a[1:2], a[5:7], a[3:4]... > > is there an elegant way to do it without any loop? Thanks! >
Yes! mapply is awesome! Thanks. Gabor Csardi wrote:> > cumsum( mapply(function(i,j) sum(a$data[i:j]), x, y) ) > > Is this what you want? > Gabor > > On Thu, Mar 13, 2008 at 06:02:13AM -0700, yoooooo wrote: >> >> Hi all, i have the following.. >> >> a <- data.frame(data = seq(1,10)) >> >> i have indices: >> x <- c(1, 5, 3, 9) >> y <- c(2, 7, 4, 10) >> >> I want the cumsum of a[1:2], a[5:7], a[3:4]... >> >> is there an elegant way to do it without any loop? Thanks! >> >> -- >> View this message in context: >> http://www.nabble.com/cumsum-list..-tp16025202p16025202.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> 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. > > -- > Csardi Gabor <csardi at rmki.kfki.hu> UNIL DGM > > ______________________________________________ > 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. > >-- View this message in context: http://www.nabble.com/cumsum-list..-tp16025202p16049054.html Sent from the R help mailing list archive at Nabble.com.