【问题标题】:Changing lubridate function to start on Monday rather than Sunday将 lubridate 函数更改为在星期一而不是星期日开始
【发布时间】:2014-10-02 11:47:03
【问题描述】:
dates <- NULL

date <- as.Date("01/01/2014","%d/%m/%Y")

dates <- data.frame(date=as.Date(character())
                    ,cal_day_in_year_num = numeric()
                    ,cal_week_id = numeric()
                    ,cal_week_start_date = as.Date(character())
                    ,cal_week_end_date = as.Date(character())
)

for (i in 1:365) {

  dates[i,1] <- date + days(i-1) ## date

  dates[i,2] <- yday(dates[i,1]) ## cal_day_in_year_num

  dates[i,3] <- paste(year(dates[i,1]),sprintf("%02d",week(dates[i,1])),sep="") ## cal_week_id

  dates[i,4] <- floor_date(dates[i,1], "week") ## cal_week_start_date

  dates[i,5] <- ceiling_date(dates[i,1], "week") ## cal_week_end_date

}

View(dates)

对于给定的日期,我正在尝试使用 lubridate 函数来计算一周的相应开始和结束日期

我遇到的问题是 lubridate 将一周的第一天定为星期日,而我需要它定为星期一 - 有没有人可以解决这个问题?

【问题讨论】:

  • 虽然这可能不是官方的方式,但您可以简单地添加+1?
  • 不幸的是,我认为将 1 添加到 cal_week_start_date 不会起作用,例如周日仍会被错误分类

标签: r lubridate


【解决方案1】:

您可以在 base.xml 中创建自己的函数来执行此操作。例如,

start.of.week <- function(date)
  date - (setNames(c(6,0:5),0:6) [strftime(date,'%w')])

end.of.week <- function(date)
  date + (setNames(c(0,6:1),0:6) [strftime(date,'%w')])

start.of.week(as.Date(c('2014-01-05','2014-10-02','2014-09-22','2014-09-27')))
# "2013-12-30" "2014-09-29" "2014-09-22" "2014-09-22"
end.of.week(as.Date(c('2014-01-05','2014-10-02','2014-09-22','2014-09-27')))
# "2014-01-05" "2014-10-05" "2014-09-28" "2014-09-28"

【讨论】:

  • 请注意,我会使用strftime(date,'%u%') 来获取使用星期一的星期几整数,但strftime 的Windows 版本不支持'%u%。这也适用于 Windows。
  • 感谢功能很棒+以后会使用strftime
【解决方案2】:

正如 Wave 所说,您只需使用 +1 即可更改日期。您也可以像以前那样做同样的事情,而不需要缓慢的for 循环。

date <- as.Date("01/01/2014","%d/%m/%Y")

yday(date) <-yday(date) + 0:364
dates <- data.frame(date = date,
                    cal_day_in_year_num = yday(date),
                    cal_week_id = paste(year(date),sprintf("%02d",week(date)),sep=""),
                    cal_week_start_date = NA,
                    cal_week_end_date = NA
                    )

# find the minimum date in the time series
min_date <- min(dates$date)
# find the previous Monday of the minimum date
for(i in 1:7){
  if(wday(min_date-i, label=TRUE)=="Mon"){
    start_date <- min_date-i
  }
}

# get the number of days between the minimum date and all days
diff_days <- as.numeric(difftime(dates$date,start_date, units="days"))
# Remove the excess days from each date
dates$cal_week_start_date <- dates$date-diff_days%%7
# Fix up the end of the week based on the start of the week
dates$cal_week_end_date <- dates$cal_week_start_date+7

【讨论】:

  • 嗨,很遗憾,这不起作用,请在您的示例中查看 1 月 5 日。由于星期日被认为是一周内的开始和结束日期计算得到拆分
  • 您可以使用 ifelse 语句解决星期天问题: dates$cal_week_start_dat=ifelse(wday(dates$date,label=T)=="Sun",floor_date(dates$date, "week ")-6,floor_date(dates$date, "week")+1).
猜你喜欢
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-27
  • 1970-01-01
相关资源
最近更新 更多