【问题标题】:Set up data in order to use Prophet() in R设置数据以便在 R 中使用 Prophet()
【发布时间】:2019-10-10 12:04:07
【问题描述】:

我想在 R 中使用 Prophet() 函数,但我无法将我的列“YearWeek”转换为 as.Date() 列。

我有一列“YearWeek”,它存储从 201401 到 201937 的值,即从 2014 年第 1 周到 2019 年第 37 周。

我不知道如何将该列声明为使用 Prophet() 函数所需的 yyyy-ww 格式的日期。

有人知道怎么做吗?

提前谢谢你。

【问题讨论】:

    标签: r date declare week-number facebook-prophet


    【解决方案1】:

    一种解决方案是将01 附加到yyyy-ww 格式化日期的末尾。

    数据:

    library(tidyverse)
    
    df <- cross2(2014:2019, str_pad(1:52, width = 2, pad = 0)) %>%
      map_df(set_names, c("year", "week")) %>%
      transmute(date = paste(year, week, sep = "")) %>% 
      arrange(date)
    
    head(df)
    #> # A tibble: 6 x 1
    #>   date  
    #>   <chr> 
    #> 1 201401
    #> 2 201402
    #> 3 201403
    #> 4 201404
    #> 5 201405
    #> 6 201406
    

    现在让我们附加 01 并转换为日期:

    df %>% 
      mutate(date = paste(date, "01", sep = ""),
             new_date = as.Date(date, "%Y%U%w"))
    #> # A tibble: 312 x 2
    #>    date     new_date  
    #>    <chr>    <date>    
    #>  1 20140101 2014-01-05
    #>  2 20140201 2014-01-12
    #>  3 20140301 2014-01-19
    #>  4 20140401 2014-01-26
    #>  5 20140501 2014-02-02
    #>  6 20140601 2014-02-09
    #>  7 20140701 2014-02-16
    #>  8 20140801 2014-02-23
    #>  9 20140901 2014-03-02
    #> 10 20141001 2014-03-09
    #> # ... with 302 more rows
    

    reprex package (v0.3.0) 于 2019 年 10 月 10 日创建

    有关一年中数字星期的更多信息,请访问here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-28
      • 2013-05-15
      相关资源
      最近更新 更多