看一看?week
周是已经发生的日期和1月1日,加一间 完整7天周期数。 isoweek返回的周数为 ,因为它会出现在ISO 8601系统中,该系统使用闰星周期重试 。
使用函数isoweek会得到以下结果。
require(lubridate)
dates <- c("2000-01-01","2000-01-06","2000-01-07","2000-01-13","2000-01-14")
sapply(dates, isoweek)
2000-01-01 2000-01-06 2000-01-07 2000-01-13 2000-01-14
52 1 1 2 2
那么,你什么时候开始一周“开始”?在下面的日期功能一周开始的星期天
require(lubridate)
my_week <- function(x){
# fst monday of the same year
first_sun <- as.POSIXct(paste0(year(x),"-01-Mon"), format = "%Y-%U-%a")
(yday(x) + (7 - yday(first_sun) + 1)) %/% 7
}
dates <- seq(as.Date("2000-01-01"), as.Date("2000-01-15"), by=1)
a <- sapply(dates, my_week)
names(a) <- dates
> a
2000-01-01 2000-01-02 2000-01-03 2000-01-04 2000-01-05
0 1 1 1 1
2000-01-06 2000-01-07 2000-01-08 2000-01-09 2000-01-10
1 1 1 2 2
2000-01-11 2000-01-12 2000-01-13 2000-01-14 2000-01-15
2 2 2 2 2
那......真的很烦人......谢谢! – 2014-11-25 02:31:18