How to get start date of week from date in R

I have a dataset with a column containing dates. I want to find the start dates of the week for these values date

.

I am getting the week number using the function week

from lubridate

. For example,

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

#Solution
[1] 16

      

Instead weeknum

, is there a way to get the start date of the week? In this case, I expect either "04/16/2017" or "04/17/2017". I don't really like it if the week starts on Sunday or Monday. I looked at this question but didn't get much from it.

+3


source to share


2 answers


Use a function floor_date

.



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

      

+5


source


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"

      

+6


source







All Articles