[R] confused with yearmon, xts and maybe zoo

Gabor Grothendieck ggrothendieck at gmail.com
Sun Apr 18 21:00:48 CEST 2010


On Sun, Apr 18, 2010 at 2:51 PM, simeon duckworth
<simeonduckworth at gmail.com> wrote:
> Hi Gabor
>
> Thats odd. I still get the same problem with the same versions of the
> software in your mail ... viz "as.yearmon" converts 2009(1) to "Dec-2008"

We can`t conclude that its in as.yearmon based on the output shown.
What is the output of:

   index(z)
   as.yearmon(index(z))
   x

This is what I get:

> index(z)
 [1] 2009.000 2009.083 2009.167 2009.250 2009.333 2009.417 2009.500 2009.583
 [9] 2009.667 2009.750 2009.833 2009.917 2010.000 2010.083 2010.167 2010.250
[17] 2010.333 2010.417 2010.500 2010.583 2010.667 2010.750 2010.833 2010.917
> as.yearmon(index(z))
 [1] "Jan 2009" "Feb 2009" "Mar 2009" "Apr 2009" "May 2009" "Jun 2009"
 [7] "Jul 2009" "Aug 2009" "Sep 2009" "Oct 2009" "Nov 2009" "Dec 2009"
[13] "Jan 2010" "Feb 2010" "Mar 2010" "Apr 2010" "May 2010" "Jun 2010"
[19] "Jul 2010" "Aug 2010" "Sep 2010" "Oct 2010" "Nov 2010" "Dec 2010"
> head(x)
         x
Jan 2009 1
Feb 2009 2
Mar 2009 3
Apr 2009 4
May 2009 5
Jun 2009 6



> and although xts is indexed at "Jan 2009" in xx, using it to create another
> xts object with that index reverts to "Dec-2008".
>
> grateful for any suggestions
>
> ## code ##
> library(xts)
> z <- zooreg(1:24,frequency=12,start=c(2009,1))
> x <- xts(z,as.yearmon(index(z)))
> xx <- x[-1, ]
> index(xx)
> xxx <- xts(NA[1:length(xx)],index(xx))
> periodicity(x)
> periodicity(xx)
> periodicty(xxx)b
>
> ## results ###
>> periodicity(x)
> Monthly periodicity from Dec 2008 to Nov 2010
>> periodicity(xx)
> Monthly periodicity from Jan 2009 to Nov 2010
>> periodicity(xxx)
> Monthly periodicity from Dec 2008 to Oct 2010
>>
>> R.version.string
> [1] "R version 2.10.1 (2009-12-14)"
>> win.version()
> [1] "Windows XP (build 2600) Service Pack 3"
>> packageDescription("xts")$Version
> [1] "0.7-0"
>> Sys.time()
> [1] "2010-04-18 19:37:26 BST"
>
>
>
> On Sun, Apr 18, 2010 at 1:25 PM, simeon duckworth <simeonduckworth at gmail.com
>> wrote:
>
>> R-listers,
>>
>> I am using xts with a yearmon index, but am getting some inconsistent
>> results with the date index when i drop observations (for example by using
>> na.omit).
>>
>> The issue is illustrated in the example below.  If I start with a monthly
>> zooreg series starting in 2009, yearmon converts this to "Dec-2008".  Not
>> such a worry for my example, but strange.  Having converted to xts, i drop
>> the first observation.  The index shows jan 2009.  But if i create a new
>> variable with this index, it shifts the series back to dec 2008.
>>
>> No doubt i am doing something wrong.  very grateful for any tips
>>
>> library(xts)
>>
>> z <- zooreg(1:24,frequency=12,start=c(2009,1))  # monthly data starting
>> 2009
>> x <- xts(z,as.yearmon(index(z)))                        # starts Dec 2008
>> xx <- x[-1,  ]                                                      # drop
>> first obs (eg through na.omit)
>> index(xx)                                                         # starts
>> jan 2009
>> xxx <- xts(NA[1:length(xx)],index(xx))                # back to dec 2008
>>
>> periodicity(x)
>> periodicity(xx)
>> periodicty(xxx)
>>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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