有人能帮帮我吗 如何获得下一年的周数的连续值。 例如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,...开始年度变化
由于
答案 0 :(得分: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 ...
答案 1 :(得分:0)
你可以试试这个
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
#.. .......... . . ..