【问题标题】:get the continuous value of week number for the next following year获取下一年周数的连续值
【发布时间】:2019-08-21 16:22:34
【问题描述】:

谁能帮帮我, 如何获得下一年周数的连续值。 例如 50,51,52,53,54,55,56 ...

下面是我的代码:

now <- as.Date(Sys.time())
dates <- seq(now, now + 900, by = "1 day") 
dat <- data.frame(Dates = dates, Week = format(dates, format = "%W"))
dat

我希望值继续为 54,55,56,... 而不是从 0,1,2,.. 重新开始

谢谢

【问题讨论】:

    标签: r


    【解决方案1】:

    试试

    round(difftime(dates, now, units = "weeks"))
    
    # Time differences in weeks
    #  [1]   0   0   0   0   1   1   1   1   1   1   1   2   2   2  ...
    

    【讨论】:

      【解决方案2】:

      你可以试试这个

      library(lubridate)
      library(dplyr)
      
      Dates <- seq(today(), today() + 900, by = "days")
      Week  <- week(Dates)
      dat   <- data.frame(Dates, Week)
      
      dat %>% 
        mutate(
          cumsum = as.integer(cumsum(c(0, diff(Week) != 0))),
          Week_new = case_when(
            year(Dates) %in% year("2017-01-01") ~ Week,
            TRUE                                ~ week(min(Dates)) + cumsum
        )
      )
      

      给你

      #        Dates Week cumsum Week_new
      #.. ..........   ..      .       ..
      #45 2017-12-29   52      6       52
      #46 2017-12-30   53      7       53
      #47 2017-12-31   53      7       53
      #48 2018-01-01    1      8       54
      #49 2018-01-02    1      8       54
      #50 2018-01-03    1      8       54
      #.. ..........    .      .       ..
      
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多