0

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,

Brian Diggs
  • 57,757
  • 13
  • 166
  • 188
Beta
  • 1,638
  • 5
  • 33
  • 67
  • edit your answer and post a few rows of your data or some example data. – Maiasaura Oct 03 '12 at 16:56
  • You need to make `DAT$wk_end_dt` into a date-time classed variable. I do not think it is yet. Read ?strptime and ?format.POSIXct. – IRTFM Oct 03 '12 at 17:04
  • Thanks DWin for your comment. I tried to strptime(DAT$end_dt,"%m/%d/%Y"). It's converting. But I don't get the calculations. Initially I was doing as.Date(DAT$end_dt,"%m/%d/%Y"). Then also I was getting the same result as strptime. I also tried POSIXct. I got the idea from following post : http://stackoverflow.com/questions/2254986/how-to-subtract-days-in-r – Beta Oct 03 '12 at 17:29

1 Answers1

2
df <- read.table(textConnection("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"), header = T)

# Then typecast your dates to a date format
df$Begin_DT <- as.Date(strptime(df$Begin_DT, "%m/%d/%Y"))
df$End_DT <- as.Date(strptime(df$End_DT, "%m/%d/%Y"))

df$Week_begin <- df$Begin_DT - 2
df$Week_end <- df$Week_begin + 6

library(plyr)
library(lubridate)
# If you dont have these packages, 
# run install.packages('plyr') and install.packages('lubridate')
ddply(df, .(Week_end), function(x) {
     if(day(x$Week_end) !=6 && day(x$Week_end) == 7) {
                x$Week_end <- x$Week_end + 6
            } else {
                x$Week_end <- x$Week_end + 6 - wday(x$Week_end)
            }
     x
    })
Maiasaura
  • 32,226
  • 27
  • 104
  • 108