On Jul 3, 2009, at 9:21 AM, rory.wins...@gmail.com wrote:
Hi
I have a dataset that looks like this (dput'd below):
head(x, 20)
time status
1 2009-07-02 10:32:37 1
2 2009-07-02 10:32:43 0
3 2009-07-02 10:32:43 1
4 2009-07-02 10:32:44 0
5 2009-07-02 10:32:44 1
6 2009-07-02 10:32:48 0
7 2009-07-02 10:32:48 1
8 2009-07-02 10:32:54 0
9 2009-07-02 10:33:04 1
10 2009-07-02 10:33:04 0
11 2009-07-02 10:33:05 1
12 2009-07-02 10:33:07 0
13 2009-07-02 10:33:13 1
14 2009-07-02 10:33:17 0
15 2009-07-02 10:33:40 1
16 2009-07-02 10:33:48 0
17 2009-07-02 10:33:50 1
18 2009-07-02 10:33:51 0
19 2009-07-02 10:33:52 1
20 2009-07-02 10:33:52 0
I would like to be able to calculate the total time spent in state
0, in
other words
the diff of the times of x where x$status changes from 0 to 1. I've
been
struggling
with tapply() to do this, but without huge success....anyone know an
elegant way
to do this?
> x <- structure(list(time = structure(c(1246527157, 1246527163,
1246527163,
1246527164, 1246527164, 1246527168, 1246527168, 1246527174,
1246527184,
1246527184, 1246527185, 1246527187, 1246527193, 1246527197,
1246527220,
1246527228, 1246527230, 1246527231, 1246527232, 1246527232), class =
c("POSIXt",
"POSIXct"), tzone = ""), status = c(1, 0, 1, 0, 1, 0, 1, 0, 1,
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0)), .Names = c("time", "status"
), row.names = c(NA, 20L), class = "data.frame")
x$wanted <- c(0,-diff(x$status))
x$dif.tm <- c(diff(x$time),NA)
sum(x[x$wanted==1, "dif.tm"], na.rm=TRUE)
# [1] 43
Cheers
-- Rory
structure(list(time = structure(c(1246527157, 1246527163, 1246527163,
1246527164, 1246527164, 1246527168, 1246527168, 1246527174,
1246527184,
1246527184, 1246527185, 1246527187, 1246527193, 1246527197,
1246527220,
1246527228, 1246527230, 1246527231, 1246527232, 1246527232), class =
c("POSIXt",
"POSIXct"), tzone = ""), status = c(1, 0, 1, 0, 1, 0, 1, 0, 1,
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0)), .Names = c("time", "status"
), row.names = c(NA, 20L), class = "data.frame")
[[alternative HTML version deleted]]
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.