padr in R: padding at user-defined interval

那年仲夏 提交于 2019-12-04 20:13:29

New version hit CRAN yesterday. You can now use units different from 1 in each of the intervals

library(padr)
library(dplyr)
coffee %>% thicken("5 min") %>% select(-time_stamp) %>% pad()

Try using the function to pad to the minute then aggregate to the specification you'd like after. This then leads to a custom summary

library(padr)
account <- data.frame(day     = as.Date(c('2016-10-21', '2016-10-23', '2016-10-26')),
                      balance = c(304.46, 414.76, 378.98))

account %>% 
  pad('min') %>%   ##pad to the minute
  mutate(five_min = cut(day, "5 min")) %>%   ##create new 'five_min' column
  group_by(five_min) %>%     ## group by the new col
  summarise(ttl = sum(balance, na.rm=TRUE))  ##aggregate the new sum
# # A tibble: 1,441 × 2
#               five_min    ttl
#                 <fctr>  <dbl>
# 1  2016-10-21 00:00:00 304.46
# 2  2016-10-21 00:05:00   0.00
# 3  2016-10-21 00:10:00   0.00
# 4  2016-10-21 00:15:00   0.00
# 5  2016-10-21 00:20:00   0.00
# 6  2016-10-21 00:25:00   0.00
# 7  2016-10-21 00:30:00   0.00
# 8  2016-10-21 00:35:00   0.00
# 9  2016-10-21 00:40:00   0.00
# 10 2016-10-21 00:45:00   0.00
# # ... with 1,431 more rows
Guy

While I couldn't get Pierre's solution to run with my data format (which I didn't help in specifying), I was able to create a solution by employing Pierre's strategy in selecting a 5-minute subset of the padded 1-minute interval data. I'm excited about this new padr library and hope more functionality is added down the road.

My strategy was the following:

library(padr)
library(zoo)
dfpad <- pad(df, interval = "min") #resample timeseries df to 1 min intervals
dfpadzoo <- zoo(dfpad,order.by = dfpad$time) #convert padded df to zoo timeseries
sensStart <- start(dfpadzoo) #first time in data using zoo function
sensEnd <- end(dfpadzoo) # last time in data using zoo function
nexttime <- df$time[2] #identify the time in the second data row
#determine time interval in minutes:
tint_min <- as.double(difftime(nexttime,sensStart, tz="UTC",units="mins"))
#Generate regularly-spaced time series from the start to end of data:
timeFill <- seq(from = as.POSIXct(sensStart, tz="UTC"),
                to = as.POSIXct(sensEnd, tz="UTC"), by = 60*tint_min)
#Create subset of dfpad spaced at 5-minute intervals
sensdatazoo <- dfpadzoo[timeFill]

By converting the df to a zoo object, I was able to employ additional time series functionality found in the zoo library.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!