如何从R中的日期获取星期开始日期 [英] How to get week starting date from a date in R

查看:124
本文介绍了如何从R中的日期获取星期开始日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,其中包含一个包含日期的列.我想找到这些date值的星期开始日期.

I have a dataset with a column containing dates. I want to find the week starting dates for those date values.

我使用lubridate中的week函数获取星期数. 例如

I get the week number using week function from lubridate. For example,

week(as.Date("04/20/2017", "%m/%d/%Y"))

#Solution
[1] 16

有没有一种方法可以获取星期几的开始日期,而不是weeknum?在这种情况下,我期望的是"04/16/2017"或"04/17/2017".我不是很特别,如果一周从星期日或星期一开始. 我查看了这个问题,但是没有不能从中得到很多.

Instead of weeknum, is there a way to get the starting date of the week? In this case I am expecting either "04/16/2017" or "04/17/2017". I am not very particular if the week starts from Sunday or Monday. I looked at this question, but didn't get much from it.

推荐答案

使用floor_date函数.

floor_date(as.Date("04/20/2017", "%m/%d/%Y"), unit="week")

这篇关于如何从R中的日期获取星期开始日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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