将lubridate功能更改为在星期一而不是星期日开始 [英] Changing lubridate function to start on Monday rather than Sunday

查看:77
本文介绍了将lubridate功能更改为在星期一而不是星期日开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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函数来计算相应的一周的开始和结束日期

For given dates I'm trying to use the lubridate function to calculate the corresponding start and end dates of the week

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

The issue I'm having is that lubridate is taking the first day of the week to be Sunday, where as I need it to be Monday - does anyone have a way round this?

推荐答案

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

You can make your own functions to do this in base. For example,

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"

这篇关于将lubridate功能更改为在星期一而不是星期日开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆