[R] Converting ts into xts and subsetting

P.B. Lecavalier p.barill at gmail.com
Thu Feb 16 05:48:48 CET 2012


Thank you for your replies. That can be quite tricky indeed.

The data consists of monthly unemployment rates, which I get as a text 
file along with the information: monthly, period of beginning and end. 
So it does not come with some timestamp as I specify the time inside R.

This tweak time(X2) <- time(X2) + 6*60*60 does not work. However this 
solves the problem:
     time(X2)=as.POSIXct(time(X2))+6*60*60

But then each element in my series now show up with time too instead of 
only year/month alone.

However Sys.setenv(TZ = "GMT") works perfectly for this purpose.

Thank you for your insights!


-- 
Philippe Baril Lecavalier



On 02/15/2012 11:30 PM, Gabor Grothendieck wrote:
> On Wed, Feb 15, 2012 at 10:25 PM, P.B. Lecavalier<p.barill at gmail.com>  wrote:
>> Greetings,
>>
>> I would like to subset observations in a time series using xts, after
>> converting from ts to xts.
>>
>> X=ts(1:100, frequency=12, start=c(1976))
>> X2=as.xts(X)
>> X2["1984"]
>>
>> The output:
>>
>> Feb 1984   98
>> Mar 1984   99
>> Apr 1984  100
>>
>> What happened to January? The index is always one month off, with
>> X2["1976-01"] giving me Feb 1976. Should I set the time using something else
>> than ts?
>>
>> I know there is the window function, but the xts format is convenient for my
>> needs.
> Any of these work with your sample data:
>
> X2[ trunc(time(X2)) == 1984 ]
>
> X2[ format(time(X2), "%Y") == "1984" ]
>
> window(X2, start = 1984)
>
> window(X, start = 1984)
>
> Sys.setenv(TZ = "GMT")
> X2["1984"]
>
>



More information about the R-help mailing list