brno792
brno792

Reputation: 6799

Lubridate get date of certain day in week()

I have dates in a dataframe column formatted as character strings like 09/30/2014

I use lubridate to identify the weeks that my dates belong to:

library(lubridate)
week(mdy("09/30/2014")

gives me the week 40

I group by that week and aggregate my data. Now I want to turn that week back into a date. So how can I get the date of a certain day in that week?

Monday for example of Week 40 in 2014 would be the date 09/29/3014. So I would want to set every Week 40 to that date.

Upvotes: 3

Views: 1562

Answers (1)

agstudy
agstudy

Reputation: 121568

You can do the following for example:

## 2 for Tuesday ,39 week number, 2014 the year
as.Date("2-39-2014",'%u-%W-%Y')
"2014-09-30"

Where you have :

> %u :Weekday as a decimal number (1–7, Monday is 1).
> %W :Week of the year as decimal number (00–53) using Monday as the first day of week
> %Y :Year with century.

Upvotes: 9

Related Questions