Hi nanas!
This looks like a job for lubridate, and its wday() function.
Does this do what you intended?
library(tidyverse)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#>
#> date
#toy dataset
data <- data.frame(
date = ymd(c('2007-10-07', '2014-04-08', '2017-03-21'))
)
# date wrangling with lubridate function wday
data %>%
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
#> end_wday
#> 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