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.
Lubridate is an R package that makes it easier to work with dates and times. Below is a concise tour of some of the things lubridate can do for you. Lubridate was created by Garrett Grolemund and Hadley Wickham, and is now maintained by Vitalie Spinu.
#2 – Calculate the Number of Weeks Between Dates To determine how many weeks elapsed between two dates, we can use a simple formula to find the number of days between the dates, then divide by 7.
Use the floor_date
function from the lubridate
package.
library("lubridate")
floor_date(as.Date("04/20/2017", "%m/%d/%Y"), unit="week")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With