Search code examples
rextracttime-seriesmaxminimum

Local and global extremum points on time series data in R based on time intervals


I am quite new in R. Actually, I have a time series data and I wanted to determine extremum point in every one hour. Moreover, data include temperature and Date_time columns where Data_time is in timestamps format. The extracted points should include local and global points.

The table name is p2 with columns temperature and date_time.

  temperature   date_time
1      0.34 2007-09-30 00:01:30
2      0.32 2007-09-30 00:03:30
3      0.26 2007-09-30 00:05:30
4      0.21 2007-09-30 00:07:30
5      0.14 2007-09-30 00:09:30
6      0.17 2007-09-30 00:11:30
7      0.16 2007-09-30 00:13:30
8      0.03 2007-09-30 00:15:30
9      0.02 2007-09-30 00:17:30
10     0.01 2007-09-30 00:19:30
11    -0.06 2007-09-30 00:21:30
12    -0.01 2007-09-30 00:23:30
13    -0.10 2007-09-30 00:25:30
14    -0.10 2007-09-30 00:27:30

Solution

  • This calculates global extrema for each hour:

    p2$date_time <- as.POSIXct(df$date_time,tz="GMT")
    p2$hours <- format(df$date_time,"%Y-%m-%d %H")
    library(plyr)
    
    p2_1 <- ddply(p2,.(hours),function(x) data.frame(min.pos=x$date_time[which.min(x$temperature)],
                                             min=min(x$temperature),
                                             max.pos=x$date_time[which.max(x$temperature)],
                                             max=max(x$temperature)))
    
              hours             min.pos   min             max.pos   max
    1 2007-09-30 00 2007-09-30 00:41:31 -0.24 2007-09-30 00:01:30  0.34
    2 2007-09-30 01 2007-09-30 01:01:31 -0.25 2007-09-30 01:05:31 -0.16