【问题标题】:Combining irregular H:M:S time stamp data into hourly intervals in R将不规则的 H:M:S 时间戳数据组合成 R 中的每小时间隔
【发布时间】:2017-07-23 18:39:27
【问题描述】:

抱歉,如果已经有类似查询的答案,但我似乎找不到它!我是 R 的新手,但我决定不为此恢复到 VBA...

我的问题是关于准备使用 ses 进行预测的数据。我有一组从 Excel 导入的带有时间戳的票证数据(约 25,000 个条目):

      Number             Created        Category  Priority `Incident state` `Reassignment count` Urgency  Impact
   <dbl>              <dttm>           <chr>     <chr>            <chr>                <dbl>   <chr>   <chr>
1      1 2014-07-01 19:16:00 Software/System 5 - Minor           Closed                    0 3 - Low 3 - Low
2      2 2014-07-02 15:27:00 Software/System 5 - Minor           Closed                    0 3 - Low 3 - Low
3      3 2014-07-02 15:27:00 Software/System 5 - Minor           Closed                    0 3 - Low 3 - Low
4      4 2014-07-02 15:27:00 Software/System 5 - Minor           Closed                    0 3 - Low 3 - Low
5      5 2014-07-02 15:28:00 Software/System 5 - Minor           Closed                    0 3 - Low 3 - Low
6      6 2014-07-02 15:29:00 Software/System 5 - Minor           Closed                    0 3 - Low 3 - Low

数据没有定期间隔,因为在工作时间之外没有提出任何票证,所以我无法指定 seq()。在转换为我可以预测的时间序列之前,我需要将 Created 列子集为每小时块。我尝试将 Created 列四舍五入为小时:

modelling_messy$Created <- as.POSIXct(modelling_messy$Created,format="%Y/%m/%d %H:%M:%S", tz = "GMT")
modelling_messy$Created <- as.POSIXct(round(modelling_messy$Created, units = "hours"))

这使我的数据看起来像我想要的那样,并允许我聚合()具有相同每小时时间戳的所有条目,但是当我使用 ts() 时它会变得很模糊

# A tibble: 2 x 8
  Number             Created        Category Priority `Incident state` `Reassignment count` Urgency  Impact
   <dbl>              <dttm>           <chr>    <dbl>            <chr>                <dbl>   <chr>   <chr>
1      1 2014-07-01 19:00:00 Software/System        5           Closed                    0 3 - Low 3 - Low
2      2 2014-07-02 15:00:00 Software/System        5           Closed                    0 3 - Low 3 - Low

> myts <- ts(modelling_clean[,1:2], start = c(2014-07-01, 1), freq = 1)
> head(myts)
Time Series:
Start = 2006 
End = 2011 
Frequency = 1 
        Group.1 Number
2006 1404241200      1
2007 1404313200      5
2008 1404316800      1
2009 1404907200      8
2010 1404910800     28
2011 1404914400      1

我知道我以某种方式搞砸了 ts() 但我找不到解决方法!我希望时间数据保持为 "%Y-%m-%d %H:00:00" 或其他有用的日期/小时组合(顺便说一下,我只涵盖 2014 - 2017 年)。

非常感谢任何和所有帮助。

非常多。

编辑 感谢您的建议 - 我认为这将解决转换为时间序列的问题,但我不确定如何从我当前的 Tibble 获取 df$Created 的数据(手动编码的数据太多!)我尝试了关注但抛出错误:

> df = data.frame(Created = modelling_messy$Created),stringsAsFactors = F)
Error: unexpected ',' in "df = data.frame(Created = modelling_messy$Created),"
> df$id = seq_along(nrow(df))
Error in df$id = seq_along(nrow(df)) : 

“闭包”类型的对象不可子集

提前致谢!

【问题讨论】:

  • 关于你的编辑:你的逗号太多了,应该是:df = data.frame(Created = modelling_messy$Created,stringsAsFactors = F)
  • 我有时真的是个笨蛋。非常感谢。现在让 R 意识到我实际上已经安装了 xts :/

标签: r timestamp forecasting


【解决方案1】:

您可以使用 xts 包创建每小时时间序列,如下所示:

library(xts)

# sample data
df = data.frame(Created = c("2014-07-01 19:16:00","2014-07-02 15:27:00","2014-07-02 15:27:00","2014-07-02 15:27:00",
                "2014-07-02 15:28:00","2014-07-02 15:29:00"),stringsAsFactors = F)
df$id = seq_along(nrow(df))

# Round dates to hours
df$Created <- as.POSIXct(df$Created,format="%Y-%m-%d %H", tz = "GMT")


# Let's aggregate and create hourly data
df = aggregate(id ~ Created, df,length)
time_series = data.frame(Created= seq( min(df$Created), max(df$Created),by='1 hour'))
time_series = merge(time_series,df,by="Created",all.x=TRUE)
time_series$id[is.na(time_series$id)]=0

# create timeseries object
library(xts)
myxts = xts(time_series$id, order.by = time_series$Created)

输出:

                    [,1]
2014-07-01 19:00:00    1
2014-07-01 20:00:00    0
2014-07-01 21:00:00    0
2014-07-01 22:00:00    0
2014-07-01 23:00:00    0
2014-07-02 00:00:00    0
2014-07-02 01:00:00    0
2014-07-02 02:00:00    0
2014-07-02 03:00:00    0
2014-07-02 04:00:00    0
2014-07-02 05:00:00    0
2014-07-02 06:00:00    0
2014-07-02 07:00:00    0
2014-07-02 08:00:00    0
2014-07-02 09:00:00    0
2014-07-02 10:00:00    0
2014-07-02 11:00:00    0
2014-07-02 12:00:00    0
2014-07-02 13:00:00    0
2014-07-02 14:00:00    0
2014-07-02 15:00:00    5

它正在工作!

免责声明:这是我第一次在 R 中使用时间序列,因此可能有其他(即更好的)方法可以实现这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-08
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 2022-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多