[R] Adding up intervals by activity

Peter Alspach PAlspach at hortresearch.co.nz
Thu Feb 24 04:46:08 CET 2005


You could use rle():  Say your Activity and Interval data is in lorin, then:

tmp.rle <- rle(as.vector(lorin[,1]))[[1]]
tapply(lorin[,2], rep(1:length(tmp.rle), tmp.rle), sum)
 1  2  3 
16  8  6 


Peter Alspach

>>> Lorin Hochstein <lorin at cs.umd.edu> 24/02/05 16:18:43 >>>
Hello all,

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

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 Hochstein
Graduate Research Assistant
Experimental Software Engineering Group
Computer Science Dept., University of Maryland
email:lorin at cs.umd.edu  tel:301-405-2721

R-help at stat.math.ethz.ch mailing list
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


The contents of this e-mail are privileged and/or confidenti...{{dropped}}

More information about the R-help mailing list