Hi r-help-boun...@r-project.org napsal dne 28.07.2009 09:18:43:
> > > I tried searching but I couldn't quite find what I was looking for. > > Here's a dummy data matrix (with row and column labels): > > y > 0 1 2 3 4 > 21 3 4 8 5 5 > 22 3 6 8 6 NA > 23 4 5 11 4 3 > 24 4 2 1 4 6 > 25 6 4 4 6 6 > > I can get cumulative row sums as follows: > > cy<-t(apply(y,1,cumsum)) > > cy > 0 1 2 3 4 > 21 3 7 15 20 25 > 22 3 9 17 23 NA > 23 4 9 20 24 27 > 24 4 6 7 11 17 > 25 6 10 14 20 26 > > Which works, but this seems rather clumsy, especially the need for t(). > > Is there a better way? One that still retains row and/or column labels? > (that will also work for data frames, if possible - though of course one can > always as.data.frame() ) > > Row differences present a different problem. Here's one way to get back the > original data: > > > cbind(cy[,1],cy[,-1]-cy[,-nrow(cy)]) > 1 2 3 4 > 21 3 4 8 5 5 > 22 3 6 8 6 NA > 23 4 5 11 4 3 > 24 4 2 1 4 6 > 25 6 4 4 6 6 > > However, if I use that I lose the first column label. Is there a way to do > something like this without > losing that label? (again, if possible, that also works for data frames?) You can use apply approach with diff cbind(vvv[,1, drop=F],t(apply(vvv,1, diff))) or your construction cbind(cy[,1, drop=F],cy[,-1]-cy[,-nrow(cy)]) Do not forget drop argument which prevents losing label!!! Regards Petr And with data frames do not forget they need not have only numeric columns, even if they look like numeric. > > thanks! > > Glen_B. > > -- > View this message in context: http://www.nabble.com/Cumulative-row-sums%2C- > row-differences-tp24692986p24692986.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. ______________________________________________ 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.