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”