[R] Adding up intervals by activity

Lorin Hochstein
Thu, 24 Feb 2005 03:18:43 -0000

Hello all,

I have a data frame with the following columns: timestamp, activity,
interval.

Each row contains information about an activity that occured over a time
interval. "Timestamp" is when the interval ended, "Activity"
is a factor
that represents the type of activity, and "Interval" is a double which
represents an interval in time. The data frame is sorted by timestamp.
For example:

Timestamp            Activity  Interval
2004-02-12 08:59:08  A         5
2004-02-12 09:23:03  A         7
2004-02-12 09:56:04  A         4
2004-02-12 10:39:00  B         5
2004-02-12 11:23:06  B         3
2004-02-12 12:56:01  A         4
2004-02-12 12:59:01  A         2
...

I would like to know how much time was spent on an activity before the
activity changed (to compute time, I just care about the lengths of the
intervals, not the values of the timestamps). For example, given the
table above, I would like to get an output that looks like:


Activity      TotalTime
A             16
B             8
A             6

Is this possible to do in R without resorting to a for loop or recursion
(i.e. what's the R-ish way to do this?) I know I can use the
"diff"
function to identify which rows represent a change in activity, but is
there anything I can do with that?

Lorin

----------
Lorin Hochstein
Graduate Research Assistant
Experimental Software Engineering Group
Computer Science Dept., University of Maryland
email:lorin at cs.umd.edu  tel:301-405-2721