mardi 11 février 2020

change the weekend date to weekday date in R

I am reading in an excel file in R and calculating the date 6 months prior to the date. If the date is falls on Weekend, need to change the date to the following weekday.

for example: if date is 2020-2-7, the six months prior is 2019-08-11. Which is Sunday. How do I change the date to 2019-08-12?

I tried the following code:

date <- as.date.character("2020-2-7")
nxtd <- date-180
if(weekdays(nxtd)=="Saturday){nxtd <- date-182} else if(weekdays(nxtd)=="Sunday){nxtd <- date-181}
else{nxtd <- date-180}

this code gives an error/warning " the condition has length > 1 and only the first element will be used"

How do I resolve it?

Aucun commentaire:

Enregistrer un commentaire