0

I need to extract the timestep from my data so I can perform operations (e.g. if I want to see 3 hours of data and my timestep is 15 minutes, I need to extract 12 rows.. but if my timestep is 1 hour, I only need 3 rows, etc.)

I have imported excel timeseries data into a dataframe. My timestep (this time) is 15 mins, and in the dataframe it looks like:

my.dataframe
  Date Time, GMT-07:00 : POSIXct, format: "2017-03-17 12:00:00" "2017-03-17 
  12:15:00" ...

Ok

> as.numeric(my.dataframe[1, 1])
 [1] 1489762000

Ok, so that's a unix date, fine, I can work with that, and my timestep is therefore 900 (seconds).

But if I do it this way:

> timestep = my.dataframe[2, 1] - my.dataframe[1, 1]
> as.numeric(timestep)
[1] 15

now it's given me the number 15 (mins) instead of the number 900 (seconds)!!!!

I need this output to be consistent in units, instead of sometimes in seconds, sometimes in minutes (sometimes in hours?). How do I extract some sort of consistent units timestep from data in R so I can perform functions?

(E.g. I need to multiply or divide the timestep by other numbers, and then input that number AS A NUMBER (not a date) into functions).

Edit: difftime gives an error

> difftime(my.dataframe[2, 1], my.dateframe[1, 1], units = "secs")
Error in as.POSIXct.default(time1) : 
 do not know how to convert 'time1' to class “POSIXct”
Val
  • 1
  • 1
  • If you leave off the `as.numeric`, does the difference actually report `Time difference of 15 minutes`? – r2evans Sep 29 '17 at 21:03
  • @r2evans Yes, it does. – Rui Barradas Sep 29 '17 at 21:06
  • Use `difftime` with `units = "secs"`. Try this: `x <- as.POSIXct(c("2017-03-17 12:00:00", "2017-03-17 12:15:00"));difftime(x[2], x[1], units = "secs")`. – Rui Barradas Sep 29 '17 at 21:07
  • **This answer doesn't work as difftime doesn't work on this data....** (but thank you) _This question may already have an answer here: How to make time difference in same units when subtracting POSIXct 1 answer_ – Val Sep 29 '17 at 21:37

0 Answers0