Calculate total time for which the value was 1 for a specified time period

Aspiring Data Scientist

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.

josliber

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Get Total requests in a period of time

From Dev

Stopping a method after a specified time period

From Dev

Changing variable value after time period

From Dev

How to calculate sum from 2 period with negative sign on Joda Time

From Dev

Python: Calculate hours within a specific time period

From Dev

JavaScript - Listen for multiple events in a specified period of time

From Dev

Calculate average exchange rate for time period

From Dev

PostgreSQL Select highest value within time period

From Dev

Calculate a return for a period of time using SAS

From Dev

Calculate time for each step of a shell script and show total execution time

From Dev

Get the total logged-in time period on Windows XP?

From Dev

time period in time period (PHP)

From Dev

how to calculate "time 1" - "time 2" in JavaScript?

From Dev

Joda time - Not able to calculate months / years from Period in minutes

From Dev

Filter time values that are a set period either side of a specified time

From Dev

Prolog - Calculate the total time of a trip

From Dev

count the total time of values in textbox. i want to calculate the time of the value

From Dev

How to calculate total network traffic for a period of time for a specific application?

From Dev

Calculate the total time required using Thread Pool

From Dev

Calculate the date a period of time in the past from today

From Dev

How to calculate time period of process

From Dev

Match given time with different time period then return the value in R

From Dev

Check if the signal is on the same value for a period of time in simulink

From Dev

Let the value of data to be the same in a time period in R

From Dev

Check column string value over time period

From Dev

mongodb get sum of value within time period

From Dev

The time that falls into a time period

From Dev

Calculate total time in oracle

From Dev

How to calculate total distance and time (getDistanceMatrix)

Related Related

  1. 1

    Get Total requests in a period of time

  2. 2

    Stopping a method after a specified time period

  3. 3

    Changing variable value after time period

  4. 4

    How to calculate sum from 2 period with negative sign on Joda Time

  5. 5

    Python: Calculate hours within a specific time period

  6. 6

    JavaScript - Listen for multiple events in a specified period of time

  7. 7

    Calculate average exchange rate for time period

  8. 8

    PostgreSQL Select highest value within time period

  9. 9

    Calculate a return for a period of time using SAS

  10. 10

    Calculate time for each step of a shell script and show total execution time

  11. 11

    Get the total logged-in time period on Windows XP?

  12. 12

    time period in time period (PHP)

  13. 13

    how to calculate "time 1" - "time 2" in JavaScript?

  14. 14

    Joda time - Not able to calculate months / years from Period in minutes

  15. 15

    Filter time values that are a set period either side of a specified time

  16. 16

    Prolog - Calculate the total time of a trip

  17. 17

    count the total time of values in textbox. i want to calculate the time of the value

  18. 18

    How to calculate total network traffic for a period of time for a specific application?

  19. 19

    Calculate the total time required using Thread Pool

  20. 20

    Calculate the date a period of time in the past from today

  21. 21

    How to calculate time period of process

  22. 22

    Match given time with different time period then return the value in R

  23. 23

    Check if the signal is on the same value for a period of time in simulink

  24. 24

    Let the value of data to be the same in a time period in R

  25. 25

    Check column string value over time period

  26. 26

    mongodb get sum of value within time period

  27. 27

    The time that falls into a time period

  28. 28

    Calculate total time in oracle

  29. 29

    How to calculate total distance and time (getDistanceMatrix)

HotTag

Archive