[R-SIG-Finance] date to index

Jeffrey Ryan jeff.a.ryan at gmail.com
Mon Apr 30 22:32:38 CEST 2012


Terminology is confusing in your posts, but I think this is what you want:

> AAPL['2007-01-10', which.i=TRUE]
[1] 6

Basically will get you the index position, without the subset (and
copies).  Of course the index itself is new to R, so it involved a Malloc
within... But I am sure you are expecting that.


W.r.t to subsetting without copying... You want something more like a
'view', which isn't supported.

Jeff

On 4/30/12 3:15 PM, "Gordon Erlebacher" <gordon.erlebach at gmail.com> wrote:

>Thanks Joshua. I'll be clearer. By index into an array, I mean "array
>index".
>
>> library("quantmod")
>> getSymbols("AAPL")
>> head(AAPL)
>           AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume
>AAPL.Adjusted
>2007-01-03     86.29     86.58    81.90      83.80    44225700
>83.80
>2007-01-04     84.05     85.95    83.82      85.66    30259300
>85.66
>2007-01-05     85.77     86.20    84.40      85.05    29812200
>85.05
>2007-01-08     85.96     86.53    85.28      85.47    28468100
>85.47
>2007-01-09     86.45     92.98    85.15      92.57   119617800
>92.57
>2007-01-10     94.75     97.80    93.45      97.00   105460000
>97.00
>
>
>The array index that corresponds to the data '2007-01-03' is 1. The index
>that corresponds to index '2007-01-10' is 6.
>
>So one solution to my problem is:
>
>> AAPL <- cbind(AAPL, index=1:length(AAPL[,1]))
>
>>  idx <- function(x, date) { return(x[date]$index) }
>
>> idx(AAPL, '2007-01-10')
>           index
>2007-01-10     6
>
>
>
>************
>
>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.
>
>******************************
>
>thanks!
>
>     Gordon
>
>
>
>
>
>PROBLEM:
>
>
>> I have a xts object. Given a date, how do I get the index into the
>>>> >> > array?
>>>> >> > Is there a pre-written function?
>
>
>
>
>
>On Mon, Apr 30, 2012 at 3:56 PM, Joshua Ulrich
><josh.m.ulrich at gmail.com>wrote:
>
>> You are confusing many things.
>>
>> 1) You asked how to add an element to the middle of a sorted array
>> (the index of an xts object), not how to add a column to a data.frame.
>> 2) xts objects are not related to data.frames at all.
>> 3) As I explained in my previous email, the index on xts/zoo objects
>> is an *attribute* of the object not a column in the underlying matrix.
>>
>> Perhaps we're speaking past each other a bit.  So I'll reiterate what
>> I said in my initial response to this thread: "An example would help."
>>
>>
>> On Mon, Apr 30, 2012 at 2:46 PM, Gordon Erlebacher
>> <gordon.erlebach at gmail.com> wrote:
>> > Adding a column to a data frame can be done without copying in several
>> > languages, since all one needs is to maintain a list of pointers to
>>the
>> > columns. You suggested I add a column of integers (the indices),
>>which is
>> > the logical approach, but for very large data frames, it is a waste of
>> > memory and time. However, it works.
>> >
>> >     Gordon
>> >
>> >
>> > On Mon, Apr 30, 2012 at 3:45 PM, Gordon Erlebacher
>> > <gordon.erlebach at gmail.com> wrote:
>> >>
>> >> Hi Jeff,
>> >>
>> >> You cannot insert an element within an array without copying. But
>>let us
>> >> do something simpler:
>> >>
>> >> df = data.frame(a=1:10, b=101:110)
>> >>
>> >> a = df[5:10]
>> >>
>> >> This can be done without copying in several languages. Not R (as far
>>as
>> I
>> >> know). In R, almost everything is done without copying, even when
>> copying is
>> >> not really required. On the other hand, R has many nice properties.
>> >>
>> >>      Gordon
>> >>
>> >>
>> >>
>> >> On Mon, Apr 30, 2012 at 3:43 PM, Joshua Ulrich
>><josh.m.ulrich at gmail.com
>> >
>> >> wrote:
>> >>>
>> >>> Please reply to the list, so others can benefit.
>> >>>
>> >>> On Mon, Apr 30, 2012 at 1:07 PM, Gordon Erlebacher
>> >>> <gordon.erlebach at gmail.com> wrote:
>> >>> > Yes, I have used that technique. Do not like it because the cya
>> >>> > structure
>> >>> > must be copied, inefficient. I try to minimize copies. It is
>>obvious
>> to
>> >>> > me
>> >>> > that r maintains this index column, internally perhaps. I will
>>adopt
>> >>> > your
>> >>> > approach then. Thanks.   Gordon.
>> >>> >
>> >>> Can you explain how would it be possible to insert a new element
>>into
>> >>> a sorted array without copying?  It's not clear to me how you could
>>do
>> >>> this in any language.
>> >>>
>> >>> The index in xts/zoo objects is an attribute attached to an
>>underlying
>> >>> matrix object.  It is not possible for the index to have a different
>> >>> number of elements than rows in the underlying matrix.
>> >>>
>> >>> > On Apr 30, 2012 1:42 PM, "Joshua Ulrich" <josh.m.ulrich at gmail.com>
>> >>> > wrote:
>> >>> >>
>> >>> >> It's not clear to me what you're trying to do.  An example would
>> help.
>> >>> >>
>> >>> >> My guess is that you're looking for something like this:
>> >>> >> x <- xts(1:10, Sys.Date()-10:1)
>> >>> >> rbind(x, xts(NA_integer_,Sys.Date()-11))
>> >>> >>
>> >>> >> Best,
>> >>> >> --
>> >>> >> Joshua Ulrich  |  FOSS Trading: www.fosstrading.com
>> >>> >>
>> >>> >> R/Finance 2012: Applied Finance with R
>> >>> >> www.RinFinance.com
>> >>> >>
>> >>> >>
>> >>> >> On Mon, Apr 30, 2012 at 12:38 PM, Gordon Erlebacher
>> >>> >> <gordon.erlebach at gmail.com> wrote:
>> >>> >> > Hi,
>> >>> >> >
>> >>> >> > I have a xts object. Given a date, how do I get the index into
>>the
>> >>> >> > array?
>> >>> >> > Is there a pre-written function?
>> >>> >> >
>> >>> >> > Thanks,
>> >>> >> >
>> >>> >> >  Gordon
>> >>> >> >
>> >>> >> >        [[alternative HTML version deleted]]
>> >>> >> >
>> >>> >> > _______________________________________________
>> >>> >> > 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.
>> >>
>> >>
>> >
>>
>
>	[[alternative HTML version deleted]]
>
>_______________________________________________
>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