[R-SIG-Finance] date to index
Joshua Ulrich
josh.m.ulrich at gmail.com
Mon Apr 30 23:19:02 CEST 2012
Gordon,
On Mon, Apr 30, 2012 at 3:34 PM, Gordon Erlebacher
<gordon.erlebach at gmail.com> wrote:
>
> In any case, regardless of whether I am expressing myself correctly or not,
> I do understand the differences between xts and data.frame, and I still
> believe that there must be a way to go from date to row number when dealing
> with xts objects. In the meantime, I'll implement the inefficient solution.
>
I'm not sure you actually do understand the difference between xts and
data.frame. Here's the example you provided:
library("quantmod")
getSymbols("AAPL")
AAPL <- cbind(AAPL, index=1:length(AAPL[,1]))
idx <- function(x, date) { return(x[date]$index) }
idx(AAPL, '2007-01-10')
And then you commented:
************
To accomplish this required a cbind, where I added a column. Doesn't
this construct a new data.frame with an additional column?
I would have assumed the existence of a function in R to accomplish
this. After all, a data frame is a list of columns, each column is a
vector, which is indexed. We are not dealing with hash tables.
******************************
You refer to data.frames several times, even though there is no
data.frame in your example. You ask, "Doesn't this [cbind] construct
a new data.frame with an additional column?" No, it doesn't! It
creates a new xts object with an additional column.
Your next comment about data.frames is correct, but completely
unrelated because xts objects are completely unrelated to data.frames.
Best,
Josh
>
> Thanks for all your help and comments.
>
Glad you were finally able to get what you needed...
>
> Gordon
>
>
>
>
> On Mon, Apr 30, 2012 at 4:26 PM, Brian G. Peterson <brian at braverock.com>
> wrote:
>>
>> On Mon, 2012-04-30 at 16:15 -0400, Gordon Erlebacher wrote:
>> > I would have assumed the existence of a function in R to accomplish
>> > this.After all, a data frame is a list of columns, each column is a
>> > vector,which is indexed. We are not dealing with hash tables.
>>
>> As Josh already told you, and xts object and a data.frame have *nothing*
>> to do with each other.
>>
>> Your example first creates a data.frame of row numbers, which could be
>> easily found via which()
>>
>> Part of the difficult here is that *usually* if you are referring to row
>> numbers in an xts object, you're doing it wrong. For time series data,
>> refer to the timestamp (the actual index), not to a row number. You can
>> reference row numbers in xts, you just usually shouldn't.
>>
>>
>> --
>> Brian
>>
>
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions
> should go.
More information about the R-SIG-Finance
mailing list