for a date get the friday of the week ending

user3022875

I have this data frame:

dat = data.frame(date = as.Date(c("2016-12-01","2016-12-02","2016-12-03","2016-12-04",
                                  "2016-12-05","2016-12-06","2016-12-07","2016-12-08",
                                  "2016-12-09","2016-12-10","2016-12-11","2016-12-12",
                                  "2016-12-13","2016-12-14","2016-12-15")))

> dat
         date
1  2016-12-01
2  2016-12-02
3  2016-12-03
4  2016-12-04
5  2016-12-05
6  2016-12-06
7  2016-12-07
8  2016-12-08
9  2016-12-09
10 2016-12-10
11 2016-12-11
12 2016-12-12
13 2016-12-13
14 2016-12-14
15 2016-12-15

I'd like to add a column that is the week ending FRIDAY's date so the results would be

         date  new column
1  2016-12-01      2016-12-02
2  2016-12-02       2016-12-02
3  2016-12-03       2016-12-09
4  2016-12-04       2016-12-09
5  2016-12-05       2016-12-09
6  2016-12-06       2016-12-09
7  2016-12-07       2016-12-09
8  2016-12-08       2016-12-09
9  2016-12-09       2016-12-09
10 2016-12-10       2016-12-16
11 2016-12-11       2016-12-16
12 2016-12-12       2016-12-16
13 2016-12-13       2016-12-16
14 2016-12-14       2016-12-16
15 2016-12-15       2016-12-16

etc....what's the best way to do that?

Jaap

A solution with base R:

dat$friday <- dat$date - (as.integer(format(dat$date, '%w')) - 5)

which gives:

> dat
         date     friday
1  2016-12-01 2016-12-02
2  2016-12-02 2016-12-02
3  2016-12-03 2016-12-02
4  2016-12-04 2016-12-09
5  2016-12-05 2016-12-09
6  2016-12-06 2016-12-09
7  2016-12-07 2016-12-09
8  2016-12-08 2016-12-09
9  2016-12-09 2016-12-09
10 2016-12-10 2016-12-09
11 2016-12-11 2016-12-16
12 2016-12-12 2016-12-16
13 2016-12-13 2016-12-16
14 2016-12-14 2016-12-16
15 2016-12-15 2016-12-16

Explanation:

  • With format(dat$date, '%w') you extract the weekdaynumber from the date-column.
  • Convert that to an integer and substract 5 (the weekdaynumber of friday).
  • Substract the result from the date and you get the date of te friday of that week.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Get date from week number

From Java

Python - Get date from day of week, year, and week number

From Dev

Get date of first and last day of week knowing week number

From Dev

How to find get week duration or week range from sunday to friday in php

From Dev

Get Date in Java given Week number, week day and year

From Dev

To pull last friday date

From Dev

Get the Date of Months Week Number

From Dev

Javascript get date of next tuesday or friday (closest)

From Dev

R - How to determine the week ending date

From Dev

MomentJS get the previous friday

From Dev

Joda Time Week Ending Date

From Dev

Scripts that get weeknumber, but changes to the next week every friday

From Dev

Get Last week date ranges in php , week Monday to sunday

From Dev

Get Date of Specific Week

From Dev

Create week from friday

From Dev

How to get the dates of the current monday, wednesday and friday of the current week in excel?

From Dev

Get date of first and last day of week knowing week number

From Dev

How can I get 'day of week' for the 'Friday' only?

From Dev

JavaScript: Get Date of 3rd Friday of the Month

From Dev

Get the Date of Months Week Number

From Dev

How to get starting and ending date from last year in plsql

From Dev

Calculate the week ending date in oracle using Saturday as the week end date

From Dev

Get the week index of each date?

From Dev

Get server date to compute days until Friday

From Dev

Get Last week date ranges in php , week Monday to sunday

From Dev

Java - get Monday date of a week give a date in that week

From Dev

getting the date of monday and friday from next week

From Dev

Group by Week Number and Week Ending

From Dev

How to get the week Starting and Ending date using Week number in SQL Server?

Related Related

  1. 1

    Get date from week number

  2. 2

    Python - Get date from day of week, year, and week number

  3. 3

    Get date of first and last day of week knowing week number

  4. 4

    How to find get week duration or week range from sunday to friday in php

  5. 5

    Get Date in Java given Week number, week day and year

  6. 6

    To pull last friday date

  7. 7

    Get the Date of Months Week Number

  8. 8

    Javascript get date of next tuesday or friday (closest)

  9. 9

    R - How to determine the week ending date

  10. 10

    MomentJS get the previous friday

  11. 11

    Joda Time Week Ending Date

  12. 12

    Scripts that get weeknumber, but changes to the next week every friday

  13. 13

    Get Last week date ranges in php , week Monday to sunday

  14. 14

    Get Date of Specific Week

  15. 15

    Create week from friday

  16. 16

    How to get the dates of the current monday, wednesday and friday of the current week in excel?

  17. 17

    Get date of first and last day of week knowing week number

  18. 18

    How can I get 'day of week' for the 'Friday' only?

  19. 19

    JavaScript: Get Date of 3rd Friday of the Month

  20. 20

    Get the Date of Months Week Number

  21. 21

    How to get starting and ending date from last year in plsql

  22. 22

    Calculate the week ending date in oracle using Saturday as the week end date

  23. 23

    Get the week index of each date?

  24. 24

    Get server date to compute days until Friday

  25. 25

    Get Last week date ranges in php , week Monday to sunday

  26. 26

    Java - get Monday date of a week give a date in that week

  27. 27

    getting the date of monday and friday from next week

  28. 28

    Group by Week Number and Week Ending

  29. 29

    How to get the week Starting and Ending date using Week number in SQL Server?

HotTag

Archive