[R] Creating date seq in data frame

jim holtman jholtman at gmail.com
Thu Mar 26 01:30:38 CET 2009


try this:

> x
  x y      xDate       Date
1 1 a 2001-02-01 2001-02-01
2 2 b 2002-02-01 2002-02-01
3 3 c 2003-02-01 2003-02-01
4 4 d 2004-02-01 2004-02-01
5 5 e 2005-02-01 2005-02-01
> # create 5 copies of each row
> indx <- rep(seq(nrow(x)), each=5)
> x.dup <- x[indx,]
> # add new date column
> x.dup$Date2 <- x.dup$Date + rep(c(-2,-1,0,1,2), length=nrow(x.dup))
>
> x.dup
    x y      xDate       Date      Date2
1   1 a 2001-02-01 2001-02-01 2001-01-30
1.1 1 a 2001-02-01 2001-02-01 2001-01-31
1.2 1 a 2001-02-01 2001-02-01 2001-02-01
1.3 1 a 2001-02-01 2001-02-01 2001-02-02
1.4 1 a 2001-02-01 2001-02-01 2001-02-03
2   2 b 2002-02-01 2002-02-01 2002-01-30
2.1 2 b 2002-02-01 2002-02-01 2002-01-31
2.2 2 b 2002-02-01 2002-02-01 2002-02-01
2.3 2 b 2002-02-01 2002-02-01 2002-02-02
2.4 2 b 2002-02-01 2002-02-01 2002-02-03
3   3 c 2003-02-01 2003-02-01 2003-01-30
3.1 3 c 2003-02-01 2003-02-01 2003-01-31
3.2 3 c 2003-02-01 2003-02-01 2003-02-01
3.3 3 c 2003-02-01 2003-02-01 2003-02-02
3.4 3 c 2003-02-01 2003-02-01 2003-02-03
4   4 d 2004-02-01 2004-02-01 2004-01-30
4.1 4 d 2004-02-01 2004-02-01 2004-01-31
4.2 4 d 2004-02-01 2004-02-01 2004-02-01
4.3 4 d 2004-02-01 2004-02-01 2004-02-02
4.4 4 d 2004-02-01 2004-02-01 2004-02-03
5   5 e 2005-02-01 2005-02-01 2005-01-30
5.1 5 e 2005-02-01 2005-02-01 2005-01-31
5.2 5 e 2005-02-01 2005-02-01 2005-02-01
5.3 5 e 2005-02-01 2005-02-01 2005-02-02
5.4 5 e 2005-02-01 2005-02-01 2005-02-03
>


On Wed, Mar 25, 2009 at 2:38 PM, Ferry <fmi.mlist at gmail.com> wrote:
> Hi,
>
> I have the following type of data:
>
> myData <- data.frame(x = 1:5, y = letters[1:5], xDate =
> seq(as.Date("2001/2/1"), as.Date("2005/2/1"), by="year") )
>
>> myData
>  x y      xDate
>  1 a 2001-02-01
>  2 b 2002-02-01
>  3 c 2003-02-01
>  4 d 2004-02-01
>  5 e 2005-02-01
>
> What I need is a new column, say xDate2, that for each xDate (or for each
> unique combination of x,y, and xDate), I will have few days before and few
> days after, as follow:
>
> myData
>
> x y      xDate      xDate2
> 1 a 2001-02-01   2001-01-30
> 1 a 2001-02-01   2001-01-31
> *1 a 2001-02-01   2001-02-01*  (original row)
> 1 a 2001-02-01   2001-02-02
> 1 a 2001-02-01   2001-02-03
> 2 b 2002-02-01   2002-01-30
> 2 b 2002-02-01   2002-01-31
> *2 b 2002-02-01   2002-02-01*  (original row)
> 2 b 2002-02-01   2002-02-02
> 2 b 2002-02-01   2002-02-03
> ...
> 5 e 2005-02-01   2005-01-30
> 5 e 2005-02-01   2005-01-31
> *5 e 2005-02-01   2005-02-01 *(original row)
> 5 e 2005-02-01   2005-02-02
> 5 e 2005-02-01   2005-02-03
>
> The actual xDate on my data is not as nicely sequenced as the above example.
> But the idea is the same, that I would like to get few days before and after
> for my each xDate.
>
> I've tried tapply, by and direct assignment like myData$xDate2 <- seq(xDate
> - 5, xDate + 5, by="day") and the like, but without result.
>
> Any idea is appreciated.
>
> Thanks much,
>
> Ferry
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list