我每隔5分钟处理一次时间序列数据。一些5分钟的时间序列不见了。我想重采样数据集,用NaN值填充缺少的5分钟周期。我在这里找到了关于如何处理这个问题的很好的信息:R: Insert rows for missing dates/times。
我创建了一个data.frame "df“,其中包含一个POSIXct timeseries列"time”。
padr包中的pad函数允许用户按分钟、小时、日等来设置间隔。
间隔 返回的datetime变量的间隔。当NULL时,interval >将等于datetime变量的间隔。当指定时,它可以>仅小于输入数据的间隔。详情见。
padr的pad功能将在我的5分钟数据上创建1分钟的间隔.如何设置自己的用户定义的间隔(例如5分钟)?
发布于 2017-05-16 08:25:18
新版本昨天上市了。现在可以在每个间隔中使用与1不同的单位。
library(padr)
library(dplyr)
coffee %>% thicken("5 min") %>% select(-time_stamp) %>% pad()发布于 2017-03-03 19:31:41
试着使用这个函数到分钟,然后聚合到你想要的规范。这将导致自定义摘要。
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发布于 2017-03-03 21:46:14
虽然我无法让Pierre的解决方案运行在我的数据格式(我没有帮助指定),但我能够通过使用Pierre的策略来选择填充的1分钟间隔数据的5分钟子集来创建解决方案。我对这个新的padr库很兴奋,并希望在以后增加更多的功能。
我的策略如下:
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]通过将df转换为一个动物园对象,我能够使用动物园库中的其他时间序列功能。
https://stackoverflow.com/questions/42586650
复制相似问题