[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",...)},
  ylim=c(min(tmp$y),sum(tmp$y)))

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

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

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

Cheers

Jason
-- 
Indigo Industrial Controls Ltd.
64-21-343-545
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