[R] having trouble extracting week from chron object
Christopher W Ryan
cryan at binghamton.edu
Tue Aug 30 20:34:01 CEST 2011
Here it is with the output:
> library(chron)
> dts <- dates(c("02/27/92", "02/27/92", "01/14/92","02/28/92", "02/01/92"))
> dts
[1] 02/27/92 02/27/92 01/14/92 02/28/92 02/01/92
> dts.chron <- as.chron(dts)
> dts.chron
[1] 02/27/92 02/27/92 01/14/92 02/28/92 02/01/92
> class(dts.chron)
[1] "dates" "times"
> # all of these component extractions work:
> months(dts.chron)
[1] Feb Feb Jan Feb Feb
12 Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < ... < Dec
> weekdays(dts.chron)
[1] Thu Thu Tue Fri Sat
Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat
> years(dts.chron)
[1] 1992 1992 1992 1992 1992
Levels: 1992
> quarters(dts.chron)
[1] 1Q 1Q 1Q 1Q 1Q
Levels: 1Q < 2Q < 3Q < 4Q
> # but weeks() doesn't seem to work
> weeks(dts.chron)
Error: could not find function "weeks"
> ?weeks
No documentation for 'weeks' in specified packages and libraries:
you could try '??weeks'
> weeks
Error: object 'weeks' not found
> week
Error: object 'week' not found
>
> sessionInfo()
R version 2.13.1 (2011-07-08)
Platform: i386-pc-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] chron_2.3-42
Thanks.
--Chris Ryan
On Tue, Aug 30, 2011 at 1:54 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Aug 30, 2011, at 1:36 PM, Christopher W Ryan 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"))
>> dts
>> dts.chron <- as.chron(dts)
>> dts.chron
>> class(dts.chron)
>> # all of these component extractions work:
>> months(dts.chron)
>> weekdays(dts.chron)
>> years(dts.chron)
>> quarters(dts.chron)
>> # but weeks() doesn't seem to work
>
> It doesn't "work" for me either but since I also have the package lubridate
> loaded, and ?weeks brings up a lubridate help page, I don't feel I have any
> right to complain. I get an error message.... what did you get?:
>
> Error in Ops.dates(pieces$week, 7) : * not defined for chron objects
>
> If I were posting I would also have had the good sense to include the output
> of sessionInfo().
>
>> weeks(dts.chron)
>
> Try type just:
>
> weeks # I get a lubridate function.
>
>>
>> ?chron tells me:
>
>
> Bur what does ?weeks tell you?
>
>> . . . The functions days(), months(), quarters(),
>> years(), weeks(), weekdays(), hours(), minutes(), and seconds() take
>> any chron object as input and extract the corresponding time interval
>> . . . .
>>
>> Any advice?
>>
>> Thanks.
>>
>> Chris Ryan
>> SUNY Upstate Medical University Clinical Campus at Binghamton
>> Binghamton, NY
>>
>> ______________________________________________
>> 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.
>
> David Winsemius, MD
> West Hartford, CT
>
>
More information about the R-help
mailing list