Calculate the difference between two DATETIME values in years - datetime

I’m wondering which DolphinDB function can calculate the time difference. For example, I have two values of DATETIME type, A and B. How can I calculate the time difference between A and B in years? The output should be like 0.5 years.

Subtract variable B from A and you can get the difference in seconds. Then divide the result by 365 * 86400 (which is the total number of seconds in a day) to obtain the yearly difference. For example:
(2022.10.18T12:00:00 - 2022.04.18T00:00:00) \ (365 * 86400 )
The result is 0.50274, meaning the difference between the two dates is 0.50274 years.

Related

Average and conversion order of operation

I have a data that I need to analyse.
The data consists of a series of numbers (floating point) represent duration in milliseconds.
From the duration I need to calculate frequency of those events (occurrence in seconds). So I simply calculate it like
occurrence per second = (1000/ time in milliseconds)
Now I need to find the average occurrence of that event in seconds.
But I am not sure which would be accurate order of operation.
Should I average the duration first and then calculate the average occurrence by
average occurrence = (1000/average time)
or I should calculate the frequency for each duration and average the result?
Both case result varies a bit. So I am not sure which pne would be correct approach.
Example:
Say we are measure frame rate of a device,
Each frame take x milliseconds to draw.
From that we can say
frame per second = (1000/x)
Now if my data has 1000 duration,
Either I can average them and get a average duration of a frame and get a frame per second = (1000/average duration)
or
we calculate 1000 frame per seconds first,
frame per seconds = (1000/duration)
and average those 100 fps value?
which one is correct?
Any suggestions?
You should choose the first method: Calculate the average duration of each frame and then let avg_fps = 1000 / avg_duration_in_milliseconds, or, probably easier: avg_fps = number_of_frames / total_duration_in_seconds. This gives the same thing.
Example:
Say you have 3 frames in one second of durations 200ms, 300ms and 500ms. Since you have 3 frames in 1 second, the avg_fps is 3.
The average duration is 333.33ms which gives the right result (1000/333.33 = 3). But if you calculate the individual fps of each frame you get (1000/200 = 5), (1000/300 = 3.33) and (1000/500 = 2). The average of 5, 3.33 and 2 is 3.44 - the highest values will skew the result in the wrong direction. So choose the first method instead.

R: get the percentage of day comprised between two timestamps

I'm trying to get the percentage of the day comprised between two timestamps.
In my case, I have the following timestamps: 2020-05-03 23:30:04 and 2020-05-03 23:43:37
So my interval is 13 minutes, which means the 0.9 % of the day, approximately.
How could I calculate this?
Alright, so I've came up with this solution (in the case of a dataframe):
timeseries= timeseries %>%
dplyr::mutate(diff_min = difftime(tsEnd,tsBegin,units="mins"),
percentage_day = (diff_min / 1440) * 100)
Where tsBegin contains 2020-05-03 23:30:04 and tsEnd contains 2020-05-03 23:43:37
I divide the difference in minutes by 1440 as this is the number of minutes in a day.

Excel - How do I match a minute in time series with specified minute to return max value from another column?

I am trying to extract the max values of CO2ppm (column E) that were logged every second over 1 hour (column D) for a total of 60 minutes (rows ~3300). I have column D for time (in HH:MM:SS) and CO2ppm (numeric). I have 50 CO2 samples that I collected that correspond with a minute (e.g. I collected sample #1 at minute 20:54 in F2), but the data is logging every second within that minute, and I want the the highest CO2 ppm in that minute).
The =MAX(IF(D:D=A2,E:E)) works to return the max value CO2ppm when I use the target value as the date (A2) for the entire day of sampling, but it does not work when I try to match my target minute (F2, 20:54) with the column D (HH:MM:SS). I tried to convert this column to text using =TEXT(D:D,"H:M") so that the target value will match the values of minute, excluding all of the seconds, but with no luck.
How can I match my minute (F2) with the range of rows that have that minute (20:54:xx, column D) to find the max value in column E?
Example data:
Thank you!
An easy way to do this would be to add a helper column with the timestamp stripped of the second component.
However in case that is not an option, you could use a formula like the following, which strips out the seconds from the timestamps in column D:
=MAX(IF((D2:D5-SECOND(D2:D5)/86400)=F2,E2:E5))
Depending on your version of Excel, you may have to confirm the formula with Ctrl+Shift+Enter.

Fetch Data At every 2 Minutes SQL Lite

Please can anyone help me in fetching data from my SQL Lite Table at every 2 minutes between my start time and stop time
I have two columns Data , TimeStamp and I am filtering between two timestamp and it is working fine but what I am trying to do is to result my data at every 2 minutes interval For example my start time is 2016-12-15 10:00:00 and stop time is 2016-12-15 10:10:00 the result should be 2016-12-15 10:00:00,2016-12-15 10:02:00,2016-12-15 10:04:00 ....
Add, to your where clause, an expression that looks for 2 minute boundaries:
strftime("%s", TimeStamp) % 120 = 0
This assumes you have data on exact, 2-minute boundaries. It will ignore data between those points.
strftime("%s", TimeStamp) converts your time stamp string into a single number representing the number of seconds since Jan 1st, 1970. The % 120 does modulo arithmetic resulting in 0 every 120 seconds. If you want minute boundaries, use 60. If you want hourly, use 3600.
What's more interesting -- and I've used this -- is to take all the data between boundaries and average them together:
SELECT CAST(strftime("%s", TimeStamp) / 120 AS INTEGER) * 120 as stamp, AVG(Data)
FROM table
WHERE TimeStamp >= '2016-12-15 10:00:00' AND
TimeStamp < '2016-12-15 10:10:00'
GROUP BY stamp;
This averages all data with timestamps in the same 2-minute "bin". The second date comparison is < rather than <= because then the last bin would only average one sample whereas the other bins would be averages of multiple values. You could also add MAX(Data) and MIN(Data) columns, if you want to know how much the data changed within each bin.

Using R to subset overlapping daily sensor data

I have a data set (3.2 million rows) in R which consists of pairs of time (milliseconds) and volts. The sensor that gathers the data only runs during the day so the time is actually the milliseconds since start-up that day.
For example, if the sensor runs 12 hours per day, then the maximum possible time value for one day is 43,200,000 ms (12h * 60m * 60s * 1000ms).
The data is continually added to a single file, which means there are many overlapping time values:
X: [1,2,3,4,5,1,2,3,4,5,1,2,3,4,5...] // example if range was 1-5 for one day
Y: [voltage readings at each point in time...]
I would like to separate each "run" into unique data frames so that I could clearly see individual days. Currently when I plot the entire data set it is incredibly muddy because in fact all of the days are being shown in the single plot. Thanks for any help.
If your data.frame df has columns X and Y, you can use diff to find every time X goes down (meaning a new day, it sounds like):
df$Day = cumsum(c(1, diff(df$X) < 0))
Day1 = df[df$Day==1,]
plot(Day1$X, Day1$Y)

Resources