[R] Date operation Question in R

David Winsemius dwinsemius at comcast.net
Fri Mar 31 18:40:40 CEST 2017


> On Mar 30, 2017, at 3:16 PM, Thomas Petzoldt <thpe at simecol.de> wrote:
> 
> On 30.03.2017 23:34, Paul Bernal wrote:
>> Hello everyone,
>> 
>> Is there a way to use the function seq to generate a date sequence in
>> this kind of format: jan-2007?
> 
> format(seq(ISOdate(2017,1,1), ISOdate(2017,12,31), "months"), "%b-%Y")

But since the original one asked for a starting point of Sys.Date, on this 31st day of March, it might be useful to demonstrate that there are pifalls for the uninitiated useR. Note the many duplicate "months":

> format(seq(ISOdate(2017,1,31), ISOdate(2018,12,31), "months"), "%b-%Y")
 [1] "Jan-2017" "Mar-2017" "Mar-2017" "May-2017" "May-2017" "Jul-2017" "Jul-2017"
 [8] "Aug-2017" "Oct-2017" "Oct-2017" "Dec-2017" "Dec-2017" "Jan-2018" "Mar-2018"
[15] "Mar-2018" "May-2018" "May-2018" "Jul-2018" "Jul-2018" "Aug-2018" "Oct-2018"
[22] "Oct-2018" "Dec-2018" "Dec-2018"

-- 
David.
> 
>> 
>> Also, is there a way to change the Sys.Date() format to the one
>> mentioned above (jan-2007)?
> 
> format(Sys.Date(), "%b-%Y")
> 
> see ?strptime for details.
> 
> Thomas
> 
>> 
>> Thanks in advance for your valuable help,
>> 
>> Best regards,
>> 
>> Paul
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list