Re: [R] conditionally merging adjacent rows in a data frame

2009-12-09 Thread Nikhil Kaza
This is great!! Sqldf is exactly the kind of thing I was looking for, other stuff. I suppose you can speed up both functions 1 and 5 using aggregate and tapply only once, as was suggested earlier. But it comes at the expense of readability. Nikhil On 9 Dec 2009, at 7:59AM, Titus von de

Re: [R] conditionally merging adjacent rows in a data frame

2009-12-09 Thread Gabor Grothendieck
On Wed, Dec 9, 2009 at 7:59 AM, Titus von der Malsburg wrote: > On Wed, Dec 9, 2009 at 12:11 AM, Gabor Grothendieck > wrote: >> Here are a couple of solutions.  The first uses by and the second sqldf: > > Brilliant!  Now I have a whole collection of solutions.  I did a simple > performance compar

Re: [R] conditionally merging adjacent rows in a data frame

2009-12-09 Thread Titus von der Malsburg
On Wed, Dec 9, 2009 at 12:11 AM, Gabor Grothendieck wrote: > Here are a couple of solutions. The first uses by and the second sqldf: Brilliant! Now I have a whole collection of solutions. I did a simple performance comparison with a data frame that has 7929 lines. The results were as followin

Re: [R] conditionally merging adjacent rows in a data frame

2009-12-08 Thread Gabor Grothendieck
Here are a couple of solutions. The first uses by and the second sqldf: > Lines <- " rt dur tid mood roi x + 55 5523 200 4 subj 9 5 + 56 5523 52 4 subj 7 31 + 57 5523 209 4 subj 4 9 + 58 5523 188 4 subj 4 7 + 70 4016 264 5 indic 9 51 + 71 4016 195 5 indic 4 14" >

Re: [R] conditionally merging adjacent rows in a data frame

2009-12-08 Thread Marek Janad
I've sent last message only to Titus. Sorry :) Below my proposition: Instead of aggregate, try summaryBy from doBy package. It's much faster. And this package made my life easier :) try: summaryBy(dur+x~index, data=d, FUN=c(sum, mean)), but index should be in data.frame as I remember. I haven't

Re: [R] conditionally merging adjacent rows in a data frame

2009-12-08 Thread Titus von der Malsburg
On Tue, Dec 8, 2009 at 5:19 PM, Nikhil Kaza 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=

Re: [R] conditionally merging adjacent rows in a data frame

2009-12-08 Thread Titus von der Malsburg
On Tue, Dec 8, 2009 at 4:50 PM, Gray Calhoun wrote: > I think there might be a problem with this approach if roi, tid, rt, > and mood are the same for nonconsecutive rows. True, but I can use the index of my reshape solution. Aggregate was the crucial ingredient. Thanks both! For the record, th

Re: [R] conditionally merging adjacent rows in a data frame

2009-12-08 Thread Gray Calhoun
I think there might be a problem with this approach if roi, tid, rt, and mood are the same for nonconsecutive rows. --Gray On Tue, Dec 8, 2009 at 9:29 AM, Nikhil Kaza wrote: > How about creating an index using multiple columns. > >  a <- with(d, aggregate(dur, list(rt=rt,tid=tid,mood=mood,roi=roi

Re: [R] conditionally merging adjacent rows in a data frame

2009-12-08 Thread Nikhil Kaza
How about creating an index using multiple columns. a <- with(d, aggregate(dur, list(rt=rt,tid=tid,mood=mood,roi=roi), sum)) b <- with(d, aggregate(x, list(rt=rt,tid=tid,mood=mood,roi=roi), mean)) c <- merge(a, b, by=c("rt","tid","mood", "roi")) I suppose one could save some time by not r

[R] conditionally merging adjacent rows in a data frame

2009-12-08 Thread Titus von der Malsburg
Hi, I have a data frame and want to merge adjacent rows if some condition is met. There's an obvious solution using a loop but it is prohibitively slow because my data frame is large. Is there an efficient canonical solution for that? > head(d) rt dur tid mood roi x 55 5523 200 4 subj