Skip to content
Prev 72063 / 398500 Next

aggregate?

On 6/17/05, alex diaz <celebridades at megamail.pt> wrote:
In the example 
- dat$y[3:4] are summed and 
- dat$y[7:8] are summed 
so we assume that what is being requested is that "d" is to
be replaced by "c" and runs of any level are to be summed.

To do that:
- create xx such that a, b, c and d in dat$x are replaced with
  with 1, 2, 3 and 3 in xx.  
- in the second statement calculate a running sum except if the 
  last observation was the same as the current observation then 
  the Last Observation is Carried Forward (locf) so that all entries 
  in a run have the same number. e.g. in this case locf is
  c(1, 2, 3, 3, 4, 5, 6, 6)
- Finally the 'by' collapses dat using locf rbinds the
  resulting rows together to create a data frame.

xx <- ifelse(dat$x == "d", 3, dat$x)
locf <- cumsum(c(TRUE, xx[-1] != xx[-length(xx)]))
f <- function(x) data.frame(x=x[1,1], y=sum(x[,2]))
dat2 <- do.call("rbind", by(dat, locf, f))