[R] expanding 1:12 months to Jan:Dec

William Dunlap wdunlap at tibco.com
Thu Aug 20 18:04:46 CEST 2009


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Moshe Olshansky
> Sent: Thursday, August 20, 2009 1:21 AM
> To: r-help at r-project.org Help; Liviu Andronic
> Subject: Re: [R] expanding 1:12 months to Jan:Dec
> 
> One possible (but not very elegant) solution is:
> 
> > aa <- paste(1:12,":10:2009",sep="")
> > dd<-as.Date(aa,format="%m:%d:%Y")
> > mon <- format(dd,"%b")
> > mon
>  [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" 
> "Oct" "Nov" "Dec"

This is 'elegent' in that it gives the month names in the current
locale.  E.g., on my Windows machine:

   > invisible(Sys.setlocale(locale="Spanish"))
   > strftime(paste(sep="-", "2009", 1:12, 1), "%b")
   [1] "ene" "feb" "mar" "abr" "may" "jun" "jul" "ago" "sep" "oct" "nov" "dic"
   > invisible(Sys.setlocale(locale="German"))
   > strftime(paste(sep="-", "2009", 1:12, 1), "%b")
   [1] "Jan" "Feb" "Mrz" "Apr" "Mai" "Jun" "Jul" "Aug" "Sep" "Okt" "Nov" "Dez"

Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com 

> 
> 
> --- On Thu, 20/8/09, Liviu Andronic <landronimirc at gmail.com> wrote:
> 
> > From: Liviu Andronic <landronimirc at gmail.com>
> > Subject: [R] expanding 1:12 months to Jan:Dec
> > To: "r-help at r-project.org Help" <r-help at r-project.org>
> > Received: Thursday, 20 August, 2009, 5:14 PM
> > Dear R users
> > I would like to do some spreadsheet style expansion of
> > dates. For
> > example, I would need to obtain a vector of months. I
> > approached in an
> > obviously wrong way:
> > > paste(01:12)
> >  [1] "1"  "2"  "3"  "4"  "5" 
> > "6"  "7"  "8"  "9"  "10" "11" "12"
> > > as.Date(paste(01:12), "%m")
> >  [1] NA NA NA NA NA NA NA NA NA NA NA NA
> > 
> > to subsequently format(.., "%b"). Other than writing the
> > months
> > manually, could anyone suggest an easier way to obtain such
> > a list?
> > Liviu
> > 
> > 
> > 
> > 
> > -- 
> > Do you know how to read?
> > http://www.alienetworks.com/srtest.cfm
> > Do you know how to write?
> > http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail
> > 
> > ______________________________________________
> > 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.
> >
> 
> ______________________________________________
> 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.
> 




More information about the R-help mailing list