为每种参数组合创建未来的时间戳

我想知道如何为每种参数组合(BranchId, Hour, weekdays)创建未来的时间戳。

BranchId      Hour   weekdays     ActivityDate     Total       1          11      3      2018-02-06T00:00:00   18    1          11      3      2018-02-13T00:00:00   23    1          12      3      2018-02-06T00:00:00   15    1          12      3      2018-02-13T00:00:00   13    1          13      3      2018-02-06T00:00:00   24    1          13      3      2018-02-13T00:00:00   22

目前我只能为一种组合创建未来的时间戳,如下所示:

BranchId    Hour    weekdays    ActivityDate       Total    1        11        3     2018-02-06T00:00:00Z   18    1        11        3     2018-02-13T00:00:00Z   23    1        11        3     2018-02-20T00:00:00Z       1        11        3     2018-02-27T00:00:00Z       1        11        3     2018-03-06T00:00:00Z       1        11        3     2018-03-13T00:00:00Z   

代码如下:

min.date <- min(data$ActivityDate)max.date <- max(data$ActivityDate)unique.time <- seq(from = min.date,  to = max.date, by = "week")forecast.time <- seq(from = max.date, by = observation.freq, length.out = 4 + 1)[-1]all.time <- c(unique.time, forecast.time)all.time <- data.frame(BranchId = data$BranchId[1], Hour = data$Hour[1], weekdays = data$weekdays[1],ActivityDate = all.time)# Join the combination with original datadata <- join(all.time, data, by = c("BranchId","Hour", "weekdays", "ActivityDate"), type = "left")

当我将此代码应用于数据时,结果出现了错误,它不会为每种组合创建未来的时间戳:

BranchId    Hour    weekdays    ActivityDate      Total   1         11        3    2018-02-06T00:00:00Z    18   1         11        3    2018-02-13T00:00:00Z    23   1         12        3    2018-02-20T00:00:00Z       1         12        3    2018-02-27T00:00:00Z       1         13        3    2018-03-06T00:00:00Z       1         13        3    2018-03-13T00:00:00Z    

我是否需要生成多个函数或使用for循环来解决这个问题?


回答:

在R语言中使用complete函数

df%>%group_by(BranchId,Hour,weekdays)%>%complete(ActivityDate = seq.Date(min(ActivityDate), min(ActivityDate)+42, by="week"),fill=list(Total=0))# A tibble: 21 x 5# Groups:   BranchId, Hour, weekdays [3]   BranchId  Hour weekdays ActivityDate Total      <int> <int>    <int>       <date> <dbl> 1        1    11        3   2018-02-06    18 2        1    11        3   2018-02-13    23 3        1    11        3   2018-02-20     0 4        1    11        3   2018-02-27     0 5        1    11        3   2018-03-06     0 6        1    11        3   2018-03-13     0 7        1    11        3   2018-03-20     0 8        1    12        3   2018-02-06    15 9        1    12        3   2018-02-13    1310        1    12        3   2018-02-20     0

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注