[R] having trouble extracting week from chron object

Gabor Grothendieck ggrothendieck at gmail.com
Tue Aug 30 22:50:07 CEST 2011


On Tue, Aug 30, 2011 at 4:43 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> On Tue, Aug 30, 2011 at 1:36 PM, Christopher W Ryan
> <cryan at binghamton.edu> wrote:
>> Running R 2.13.1 on Windows XP.
>> I would like to get week of the year (1-52) for each date.
>>
>> library(chron)
>> dts <- dates(c("02/27/92", "02/27/92", "01/14/92","02/28/92", "02/01/92"))
>
> Try this:
>
> as.numeric(cut(dts, "weeks"))

although that won't label the weeks from the beginning of the year but
rather the earliest date will be labelled week 1 so you need to add a
date in the first of the year if you want that.  Alternately  try
this: as.numeric(format(as.Date(dts), "%w")) or use %W.  See ?strptime
for details.


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list