本文介绍了不包括Dot in Dplyr的周末的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是此答案的延续问题:https://stackoverflow.com/a/45254762/5893585

我在prophet包的dplyr中使用do函数。尝试此操作时,我想创建一个不包括周末的未来数据帧。以下是我当前的代码:

当前数据帧:

dataset
          ds     group     y
  2021-12-15         A     5
  2021-12-16         A     6
  2021-12-15         B    10
  2021-12-16         B     7
         etc       etc   etc

预测

library(dplyr)
library(prophet)

data = dataset %>%  
group_by(group) %>%
do(predict(prophet(., daily.seasonality = TRUE, yearly.seasonality = TRUE), 
make_future_dataframe(prophet(.,daily.seasonality = TRUE, yearly.seasonality = TRUE), periods = 14))) %>%
select(ds, group, yhat)

如何重写上述代码以筛选make_future_dataframe数据集没有周末?

我希望它看起来像这样,但这不起作用:

data = dataset %>%  
  group_by(group) %>%
  do(predict(prophet(., daily.seasonality = TRUE, yearly.seasonality = TRUE), 
  make_future_dataframe(prophet(.[which(weekdays(.$ds) != 'Saturday' | weekdays(.$ds) != 'Sunday'),],daily.seasonality = TRUE, yearly.seasonality = TRUE), periods = 14))) %>%
  select(ds, group, yhat)

推荐答案

我们可以在预测之前删除周末:

df %>% 
  group_by(group) %>% 
  mutate(weekdays = weekdays(ds)) %>% 
  filter(weekdays != "Saturday" & weekdays != "Sunday") %>% 
  do(predict(prophet(., daily.seasonality = TRUE, yearly.seasonality = TRUE), 
  filter(make_future_dataframe(prophet(., daily.seasonality = TRUE, yearly.seasonality = TRUE), periods = 14), weekdays(ds) != "Saturday" & weekdays(ds) != "Sunday"))) %>%
  select(ds, group, yhat)

这篇关于不包括Dot in Dplyr的周末的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 16:19