I've 2 date variable WK_BEGIN_DT & WK_END_DT in a dataframe, say DAT. Both these are in character format. I'm trying to get the following. I'm using the SAS command here, but want to do the same in R.
My data looks like this:
var1 Begin_DT End_DT
1 02/07/2012 02/11/2012
2 04/09/2012 04/15/2012
3 04/17/2012 04/23/2012
4 06/21/2012 06/30/2012
5 07/15/2012 07/29/2012
SAS code:
DATA DAT;
set DAT;
## PART 1: ##
WK_BEGIN_DT = BEGIN_DT - 2 ;
WK_END_DT = WK_BEGIN_DT + 6 ;
## PART 2: ##
if WEEKDAY(WK_END_DT) ^= 6 then do ;
if weekday(WK_END_DT) = 7 then WK_END_DT = WK_END_DT + 6 ;
else WK_END_DT = WK_END_DT + 6 - weekday(WK_END_DT) ;
end ;
For PART 1 problem, I tried
DAT$new.dt <- DAT$wk_end_dt - as.difftime(2, unit="days")
After the calculation, the result should look like:
var1 Begin_DT End_DT Wk_Begin_DT WK_END_DT
1 02/07/2012 02/11/2012 02/05/2012 02/12/2012
2 04/09/2012 04/15/2012 04/07/2012 04/15/2012
3 04/17/2012 04/23/2012 04/15/2012 04/23/2012
4 06/21/2012 06/30/2012 06/19/2012 06/25/2012
5 07/15/2012 07/29/2012 07/13/2012 07/21/2012
But it showing the following error:
Error in `$<-.data.frame`(`*tmp*`, "new.dt", value = numeric(0)) :
replacement has 0 rows, data has 2354
Can anybody please help me how to get the results for both PART 1 & PART 2?
Thank you. Regards,