How to get week starting date from a date in R

krish picture krish · Apr 20, 2017 · Viewed 12.6k times · Source

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.

Answer

Vamsi Prabhala picture Vamsi Prabhala · Apr 20, 2017

Use the floor_date function from the lubridate package.

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