[R] How to generate a sequence of dates without hardcoding the year

jim holtman jholtman at gmail.com
Sun Jul 25 01:02:57 CEST 2010


Is this what you want if you want to assume that the date without a
year is this year:

> seq(as.Date("7-1","%m-%d"),by="week", length=52)
 [1] "2010-07-01" "2010-07-08" "2010-07-15" "2010-07-22" "2010-07-29"
"2010-08-05" "2010-08-12" "2010-08-19"
 [9] "2010-08-26" "2010-09-02" "2010-09-09" "2010-09-16" "2010-09-23"
"2010-09-30" "2010-10-07" "2010-10-14"
[17] "2010-10-21" "2010-10-28" "2010-11-04" "2010-11-11" "2010-11-18"
"2010-11-25" "2010-12-02" "2010-12-09"
[25] "2010-12-16" "2010-12-23" "2010-12-30" "2011-01-06" "2011-01-13"
"2011-01-20" "2011-01-27" "2011-02-03"
[33] "2011-02-10" "2011-02-17" "2011-02-24" "2011-03-03" "2011-03-10"
"2011-03-17" "2011-03-24" "2011-03-31"
[41] "2011-04-07" "2011-04-14" "2011-04-21" "2011-04-28" "2011-05-05"
"2011-05-12" "2011-05-19" "2011-05-26"
[49] "2011-06-02" "2011-06-09" "2011-06-16" "2011-06-23"
>


On Sat, Jul 24, 2010 at 5:07 PM, Felipe Carrillo
<mazatlanmexico at yahoo.com> wrote:
> Hi:
> I have a dataframe named 'spring' and I am trying to add a new variable named
> 'IdDate'
> This line of code works fine:
> spring$idDate <- seq(as.Date("2008-07-01"),as.Date("2009-06-30"),by="week")
>
> But I don't want to hardcode the year because it will be used again the
> following year
> Is it possible to just generate dates with the month and day?
>
> I tried the code below:
> seq(as.Date("7-1","%B%d"),as.Date("6-30","%B%d"),by="week")
>
> and got this error message:
> Error in seq.int(0, to - from, by) : 'to' must be finite
> Thanks for any pointers
>
>
> Felipe D. Carrillo
> Supervisory Fishery Biologist
> Department of the Interior
> US Fish & Wildlife Service
> California, USA
>
>
>
>
> ______________________________________________
> 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