[R] cumsum and subsets of a data frame?

Jason Turner jasont at indigoindustrial.co.nz
Thu Jul 25 00:27:40 CEST 2002

On Wed, Jul 24, 2002 at 09:29:28AM -0500, Michael A. Miller wrote:
> I'm plotting things like this will lattice
> > library(lattice)
> > xyplot(y ~ x | f, data=tmp)
> If I plot the cumsum with xyplot( cumsum(y) ~ x | f, data=tmp),
> it is summed across the values of the factor f.  

cumsum(y) is evaluated first, then xyplot.

There have been replies that suggested fiddling with the data.frame
to make a new data.frame.  You could also fiddle with the panel 
argument of xyplot.  Something like...

xyplot(y~x | f, data=tmp, 
  panel=function(x,y,...) {panel.xyplot(x,cumsum(y),type="l",...)})

that clips vertically - apparently, the ylim is the (quite sensible) range
of the y data.  So....

xyplot(y~x | f, data=tmp,
  panel=function(x,y,...) {panel.xyplot(x,cumsum(y),type="l",...)},

or, if you want the points too....

xyplot(y~x | f, 
    panel=function(x,y,...) {

It won't return the calculated cumsum value, but it'll look pretty,
and work for any arrangement of your "split" variable.


Indigo Industrial Controls Ltd.
jasont at indigoindustrial.co.nz
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list