This looks like a job for lubridate, and its
Does this do what you intended?
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
data <- data.frame(
date = ymd(c('2007-10-07', '2014-04-08', '2017-03-21'))
# date wrangling with lubridate function wday
mutate(start_shift = 2 - wday(date), # days away from Monday
end_shift = 6 - wday(date), # days away from Friday
start_date = date + start_shift, # shift to Monday
end_date = date + end_shift, #shift to Friday
start_wday = wday(start_date, label = TRUE), # check = Monday
end_wday = wday(end_date, label = TRUE)) # check = Friday
#> date start_shift end_shift start_date end_date start_wday
#> 1 2007-10-07 1 5 2007-10-08 2007-10-12 Mon
#> 2 2014-04-08 -1 3 2014-04-07 2014-04-11 Mon
#> 3 2017-03-21 -1 3 2017-03-20 2017-03-24 Mon
#> 1 Fri
#> 2 Fri
#> 3 Fri
Created on 2019-11-08 by the reprex package (v0.3.0)
and here is a how-to on how to write your own reprex - it makes answering questions easier
Getting unstuck is hard. Your first step here is usually to create a reprex, or reproducible example. The goal of a reprex is to package your code, and information about your problem so that others can run it and feel your pain. Then, hopefully, folks can more easily provide a solution.
What's in a Reproducible Example?
Parts of a reproducible example:
background information - Describe what you are trying to do. What have you already done?
complete set up - include any library() calls and data to reproduce your issue.
data for a reprex:
Here's a discussion on setting up data for a reprex
make it run - include the minimal code required to reproduce your error on the data p…