[R] Time Series Have Date Show Days of the Week
G See
gsee000 at gmail.com
Sat Aug 11 18:41:52 CEST 2012
Alternatively, xts has a convenience function for this
.indexwday(SPY)
will give weekdays as numbers with Sunday being 0 and Saturday being 6.
There are also several similar functions
.indexDate(x)
.indexday(x)
.indexmday(x)
.indexwday(x)
.indexweek(x)
.indexmon(x)
.indexyday(x)
.indexyear(x)
.indexhour(x)
.indexmin(x)
.indexsec(x)
Regards,
Garrett
On Wed, Aug 1, 2012 at 6:04 PM, R. Michael Weylandt
<michael.weylandt at gmail.com> wrote:
> On Wed, Aug 1, 2012 at 12:54 PM, Douglas Karabasz
> <douglas at sigmamonster.com> wrote:
>> I used quantmod to pull in price data from the ticker SPY. The data has
>> date and closing price. I would like to show the day of the week for each
>> closing price. Is that possible? Also, I would like to add the back into
>> the data frame in a new column without changing the structure of the data
>> set if possible.
>>
>>
>>
>> SPY
>>
>> 2009-01-02 92.96
>>
>> 2009-01-05 92.85
>>
>> 2009-01-06 93.47
>>
>> 2009-01-07 90.67
>>
>> 2009-01-08 91.04
>>
>> 2009-01-09 89.09
>>
>>
>>
>>> str(SP500)
>>
>> An 'xts' object from 2009-01-02 to 2012-07-31 containing:
>>
>> Data: num [1:902, 1] 93 92.8 93.5 90.7 91 ...
>>
>> - attr(*, "dimnames")=List of 2
>>
>> ..$ : NULL
>>
>> ..$ : chr "SPY"
>>
>> Indexed by objects of class: [Date] TZ:
>>
>> xts Attributes:
>>
>> List of 4
>>
>> $ tclass : chr [1:2] "POSIXct" "POSIXt"
>>
>> $ tzone : chr ""
>>
>> $ src : chr "yahoo"
>>
>> $ updated: POSIXct[1:1], format: "2012-07-31 17:59:16"
>>
>
> Hi Doug,
>
> No, this isn't quite doable. I'll give a somewhat technical
> description of why and then I'll propose a work around
>
> ---- Technical Stuff ---
>
> An "xts" object consists of two fundamental parts -- an index which is
> numeric (seconds since the epoch usually) and its "coredata" which is,
> in your case, the prices. The "coredata" is and must be all of the
> same "type" -- either integer, double, or string; internally, this is
> because it's all actually a matrix, which is in turn an atomic vector,
> and to be any sort of performant, we need them all of the same type.
> So there's no way for the coredata to have the _number_ 24 and the
> _string_ day of the week "tuesday". We'll come back to this though.
>
> So you might ask about the index... xts hard-codes selected index
> classes to work. None of them currently have printing methods that
> write out the day of the week like you want, though you could define
> your own time index and use it in zoo if desired. That's almost
> certainly overkill though.
>
> ---- What to do ----
>
> In light of the above, the easiest thing is probably to encode the day
> of the week as an integer if you really need it for calculations:
>
> as.numeric(factor(strptime(index(x), "%A")))
>
> will create that and then you can cbind() it on.
>
> Alternatively, you can cbind() just strptime(index(... on and you will
> change the coredata() to character. I'd do this instead if you're only
> looking for human output. Even better on the "just look" pretty front
> would be to make it into a data.frame for printing only:
>
> data.frame(x, `Day of the Week` = strptime(index(x), "%A"))
>
> but again -- that's only for printing: it will destroy the "xts"-ness.
>
> Best,
> Michael
>
>>
>>
>> Thank you,
>>
>> Douglas
>>
>>
>>
>>
>> [[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.
>
> ______________________________________________
> 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