[R] conditionally merging adjacent rows in a data frame

Titus von der Malsburg malsburg at gmail.com
Tue Dec 8 23:13:46 CET 2009


On Tue, Dec 8, 2009 at 5:19 PM, Nikhil Kaza <nikhil.list at gmail.com> wrote:
> I suppose that is true, but the example data seem to suggest that it is
> sorted by rt.

I was not very clear on that.  Sorry.

> d$count <- 1
>  a <- with(d, aggregate(subset(d, select=c("dur", "x", "count"),
> list(rt=rt,tid=tid,mood=mood,roi=roi), sum))
> a$x <- a$x/a$count

This is neat!

> But it would still be nice to get a generic way that uses different
> functions on different columns much like excel's pivot table.

I guess the most straight-forward thing would be to extend aggregate
to also accept instead of a FUN a list of FUNs where the first is
applied to value of the first column of x (the data frame), the second
to the second column, and so on.

  Titus




More information about the R-help mailing list