Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get week starting date from a date in R [duplicate]

Tags:

date

r

lubridate

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.

like image 214
krish Avatar asked Apr 20 '17 13:04

krish


People also ask

What is Lubridate R?

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.

How do you calculate week number?

#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.


1 Answers

Use the floor_date function from the lubridate package.

library("lubridate")
floor_date(as.Date("04/20/2017", "%m/%d/%Y"), unit="week")
like image 187
Vamsi Prabhala Avatar answered Oct 25 '22 01:10

Vamsi Prabhala