I have a dataset with two columns. I need to calculate the total time in seconds for which the value was greater than 1 for the duration between 00:00 to 6:00. How can I do this in most efficient way in R? Can this be done using dplyr package? I need to do this in a generic way such that it can be applied for other durations(6 to 9, 9 to 12) as well. Below is some sample data :
+--------------------------------------+
| Timestamp Value |
+--------------------------------------+
| 2015-10-01 00:00:00 300 |
| 2015-10-01 00:00:55 200 |
| 2015-10-01 00:25:10 0 |
| 2015-10-01 01:05:40 876 |
| 2015-10-01 02:05:40 989 |
| 2015-10-01 04:05:40 0 |
| 2015-10-01 05:00:00 600 |
| 2015-10-01 06:00:00 300 |
+--------------------------------------+
So the output that is expected here for duration between 00 to 06 is 15910 seconds.
First I would parse the date/time:
dat$Timestamp <- strptime(dat$Timestamp, format="%Y-%m-%d %H:%M:%S")
Then I would grab the seconds between each observation using difftime
:
secs <- as.numeric(difftime(tail(dat$Timestamp, -1), head(dat$Timestamp, -1),
units="secs"))
Finally, I would sum up the number of seconds in each interval that has value greater than 1:
sum(secs[head(dat$Value, -1) > 1])
# [1] 15910
Assuming the boundaries of the time you are interested in appear in the Timestamp field, you can limit to the time range of interest (start at begin.time
and end at end.time
) with something like:
dat.subset <- dat[dat$Timestamp >= begin.time & dat$Timestamp <= end.time,]
Data:
dat <- data.frame(Timestamp = c("2015-10-01 00:00:00", "2015-10-01 00:00:55", "2015-10-01 00:25:10", "2015-10-01 01:05:40", "2015-10-01 02:05:40", "2015-10-01 04:05:40", "2015-10-01 05:00:00", "2015-10-01 06:00:00"), Value = c(300, 200, 0, 876, 989, 0, 600, 300))
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments