Summarising grouped records in a dataframe in R

Posted by monch1962 on Stack Overflow See other posts from Stack Overflow or by monch1962
Published on 2010-04-15T04:43:10Z Indexed on 2010/04/15 6:43 UTC
Read the original article Hit count: 260

Filed under:
|
|
|

Hello all,

I have a data frame in R that looks like this:

> TimeOffset, Source, Length 
> 0         1           1500
> 0.1       1           1000    
> 0.2       1           50
> 0.4       2           25
> 0.6       2           3
> 1.1       1           1500
> 1.4       1           18
> 1.6       2           2500
> 1.9       2           18
> 2.1       1           37
> ...

and I want to convert it to

> TimeOffset, Source, Length
> 0.2         1       2550
> 0.6         2       28
> 1.4         1       1518
> 1.9         2       2518
> ...

Trying to put this into English, I want to group consecutive records with the same 'Source' together, then printing out a single record per group showing the highest time offset in that group, the source, and the sum of the lengths in that group.

The TimeOffset values will always increase.

I suspect this is possible in R, but I really don't know where to start. In a pinch I could export the data frame out and do it in e.g. Python, but I'd prefer to stay within R if possible.

Thanks in advance for any assistance you can provide

© Stack Overflow or respective owner

Related posts about statistics

Related posts about r