如何将floor_date()舍入为任意日期? [英] How to round floor_date() to arbitrary date?

查看:363
本文介绍了如何将floor_date()舍入为任意日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用floor_date将日期四舍五入为星期几:

I am using floor_date to round dates to the weeks:

library(lubridate)
floor_date(ymd_hms('2016-08-26 16:27:15'), '7 days')
[1] "2016-08-22 UTC"

但是,我想将一个特定的日期设置为第一周的开始. 例如,我想将"2016-08-26 UTC"设置为第一周.作为一种解决方法,我试图调整getOption("lubridate.week.start"),但是无论我通过什么,我都无法更改开始的工作日:

However, I would like to set a specific date as the beginning of the first week. For instance, I would like to set "2016-08-26 UTC" as first week. As a workaround I was trying to tweak getOption("lubridate.week.start"), but I cannot get to change the starting weekday regardless of what I pass:

> floor_date(ymd_hms('2016-08-26 16:27:15'), '7 days', week_start = getOption("lubridate.week.start", 1))
[1] "2016-08-22 UTC"
> floor_date(ymd_hms('2016-08-26 16:27:15'), '7 days', week_start = getOption("lubridate.week.start", 7))
[1] "2016-08-22 UTC"

推荐答案

您需要设置unit = "week".来自?round_date

week_start:当单位为周时,指定参考日; 7是星期天.

week_start: when unit is weeks specify the reference day; 7 being Sunday.

示例

library(lubridate)
lapply(c(1, 5), function(x) floor_date(ymd('2016-08-26'), 'week', week_start = x))
#[[1]]
#[1] "2016-08-22"
#
#[[2]]
#[1] "2016-08-26"

这篇关于如何将floor_date()舍入为任意日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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