Maximum slope for a given interval each day

后端 未结 1 1902
后悔当初
后悔当初 2021-01-23 16:30

I have a set of time series data with ground surface temperatures measured every 10 minutes over multiple days (actually 2 years of data) from three different locations. What I

相关标签:
1条回答
  • 2021-01-23 17:36

    You can use xts timeseries package which is very good for timeseries analysis. Combined with TTR package, you can get what you want quite easily.

    require(xts)
    require(TTR)
    dat <- read.csv("https://gist.github.com/natemiller/42eaf45747f31a6ccf9a/raw/916443cfb353d82e8af6cdebdd80b2e956317b24/sampleTempData.csv")
    
    dat.xts <- .xts(x = dat$Temp, index = as.POSIXct(strptime(dat$Date, format = "%m/%d/%y %H:%M")))
    names(dat.xts) <- "Temp"
    head(dat.xts)
    ##                     Temp
    ## 2011-04-11 03:48:00  9.5
    ## 2011-04-11 03:58:00  9.5
    ## 2011-04-11 04:08:00  9.5
    ## 2011-04-11 04:18:00  9.5
    ## 2011-04-11 04:28:00  9.5
    ## 2011-04-11 04:38:00  9.5
    
    
    dat.xts$ROC <- ROC(dat.xts, n = 6)
    head(dat.xts, 10)
    ##                     Temp ROC
    ## 2011-04-11 03:48:00  9.5  NA
    ## 2011-04-11 03:58:00  9.5  NA
    ## 2011-04-11 04:08:00  9.5  NA
    ## 2011-04-11 04:18:00  9.5  NA
    ## 2011-04-11 04:28:00  9.5  NA
    ## 2011-04-11 04:38:00  9.5  NA
    ## 2011-04-11 04:48:00  9.5   0
    ## 2011-04-11 04:58:00  9.5   0
    ## 2011-04-11 05:08:00  9.5   0
    ## 2011-04-11 05:18:00  9.5   0
    
    dat.xts[which.max(dat.xts$ROC), ]
    ##                     Temp       ROC
    ## 2011-04-12 09:48:00 14.5 0.5340825
    
    
    # If you want to do analysis on per day basis.
    dat.xts <- .xts(x = dat$Temp, index = as.POSIXct(strptime(dat$Date, format = "%m/%d/%y %H:%M")))
    names(dat.xts) <- "Temp"
    head(dat.xts)
    ##                     Temp
    ## 2011-04-11 03:48:00  9.5
    ## 2011-04-11 03:58:00  9.5
    ## 2011-04-11 04:08:00  9.5
    ## 2011-04-11 04:18:00  9.5
    ## 2011-04-11 04:28:00  9.5
    ## 2011-04-11 04:38:00  9.5
    
    
    ll <- split.xts(dat.xts, f = "days")
    
    
    ll <- lapply(ll, FUN = function(x) {
        x$ROC <- ROC(x, 6)
        return(x)
    })
    
    max.ll <- lapply(ll, function(x) x[which.max(x$ROC), ])
    
    max.ll
    ## [[1]]
    ##                     Temp       ROC
    ## 2011-04-11 13:38:00 20.5 0.4946962
    ## 
    ## [[2]]
    ##                     Temp       ROC
    ## 2011-04-12 09:48:00 14.5 0.5340825
    ## 
    ## [[3]]
    ##                     Temp       ROC
    ## 2011-04-13 10:18:00 15.5 0.4382549
    ## 
    ## [[4]]
    ##                     Temp       ROC
    ## 2011-04-14 10:38:00 14.5 0.3715636
    
    0 讨论(0)
提交回复
热议问题