问题
I have a dataset with a column containing dates. I want to find the week starting dates for those date
values.
I get the week number using week
function from lubridate
.
For example,
week(as.Date("04/20/2017", "%m/%d/%Y"))
#Solution
[1] 16
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.
回答1:
Use floor_date
function.
floor_date(as.Date("04/20/2017", "%m/%d/%Y"), unit="week")
回答2:
You can use below
as.Date(format(as.Date("04/20/2017", "%m/%d/%Y"),"%Y-%W-1"),"%Y-%W-%u")
[1] "2017-04-17"
来源:https://stackoverflow.com/questions/43521371/how-to-get-week-starting-date-from-a-date-in-r