forgot this part:
frmla = as.formula(paste(fields,"~","ticker"))
dat2<- unstack(dat1, frmla)

dat = do.call(cbind, lapply(dat2, function(.vec){

>
>                     if(length(.vec) >= 20){
>                                                     .vec[1:20]
>                                                   }else if(length(.vec) <
> 20){
>                                                     c(rep(NaN, 20 -
> length(.vec)),.vec)
>                                                   }
>                                                 }
>                             )
>               )



On Tue, Jul 3, 2012 at 12:32 PM, Ben quant <ccquant@gmail.com> wrote:

> dat = do.call(cbind, lapply(dat2, function(.vec){
>                                                   if(length(.vec) >= 20){
>                                                     .vec[1:20]
>                                                   }else if(length(.vec) <
> 20){
>                                                     c(rep(NaN, 20 -
> length(.vec)),.vec)
>                                                   }
>                                                 }
>                             )
>               )
>
>
>
> On Tue, Jul 3, 2012 at 11:21 AM, Ben quant <ccquant@gmail.com> wrote:
>
>> xts endpoints uses 'existing' dates in the data. I want to use my dates
>> and put data on the correct row.
>>
>> Here are the details of some sample data...
>>
>> library('Rbbg')
>> library('stringr')
>>
>> conn <- blpConnect(throw.ticker.errors = F)
>>
>> daysback<- (261 * 7) # or whatever
>> start.date <- as.POSIXct(Sys.Date()-daysback)
>> end.date <- as.POSIXct(Sys.Date())
>> tickers<- c("A US Equity", "AA US Equity")
>>
>>  fields<-c("SALES_REV_TURN")
>> o_names = c("periodicitySelection")
>>  o_values = c("QUARTERLY")
>>
>> dat1<-bdh(conn, tickers, fields, start.date,end.date,option_names =
>> o_names, option_values = o_values,
>>           always.display.tickers = TRUE,include.non.trading.days = T)
>>
>> > dat1
>>          ticker       date SALES_REV_TURN
>> 1   A US Equity 2007-07-31           1374
>> 2   A US Equity 2007-10-31           1446
>> 3   A US Equity 2008-01-31           1393
>> 4   A US Equity 2008-04-30           1456
>> 5   A US Equity 2008-07-31           1444
>> 6   A US Equity 2008-10-31           1481
>> 7   A US Equity 2009-01-31           1166
>> 8   A US Equity 2009-04-30           1091
>> 9   A US Equity 2009-07-31           1057
>> 10  A US Equity 2009-10-31           1167
>> 11  A US Equity 2010-01-31           1213
>> 12  A US Equity 2010-04-30           1271
>> 13  A US Equity 2010-07-31           1384
>> 14  A US Equity 2010-10-31           1576
>> 15  A US Equity 2011-01-31           1519
>> 16  A US Equity 2011-04-30           1677
>> 17  A US Equity 2011-07-31           1691
>> 18  A US Equity 2011-10-31           1728
>> 19  A US Equity 2012-01-31           1635
>> 20  A US Equity 2012-04-30           1733
>> 21 AA US Equity 2007-09-30           7387
>> 22 AA US Equity 2007-12-31           5919
>> 23 AA US Equity 2008-03-31           6998
>> 24 AA US Equity 2008-06-30           7245
>> 25 AA US Equity 2008-09-30           6970
>> 26 AA US Equity 2008-12-31           5688
>> 27 AA US Equity 2009-03-31           4147
>> 28 AA US Equity 2009-06-30           4244
>> 29 AA US Equity 2009-09-30           4615
>> 30 AA US Equity 2009-12-31           5433
>> 31 AA US Equity 2010-03-31           4887
>> 32 AA US Equity 2010-06-30           5187
>> 33 AA US Equity 2010-09-30           5287
>> 34 AA US Equity 2010-12-31           5652
>> 35 AA US Equity 2011-03-31           5958
>> 36 AA US Equity 2011-06-30           6585
>> 37 AA US Equity 2011-09-30           6419
>> 38 AA US Equity 2011-12-31           5989
>> 39 AA US Equity 2012-03-31           6006
>>
>> So, for example, I'd like these two values on the same row:
>> A US Equity 2012-04-30           1733
>> AA US Equity 2012-03-31           6006
>>
>> ...and the row name would be 2012-03-31. A's value should be on the
>> 2012-03-31 row (despite having a date of 2012-04-30) because I'm putting
>> everything on 'normal' quarter end dates. Therefore, all of A's values
>> would shift back one in date. It has to be a general solution that puts
>> 'abnormal' fiscal quarter end company data on the same row as 'normal'
>> quarter end dates. (I understand this introduces look-ahead bias, but I
>> correct for it later when applicable.)  Is there a function out there
>> that does this already?
>>
>> Again here is how I produce the 'normal' fiscal quarter end dates that I
>> want as row names:
>> to_date = as.Date(Sys.time())
>> from_date = as.Date(to_date - (daysback + 100))
>>
>> get_dates <- function(Start, End,end_of,every)
>> {
>>   Vec <- as.Date(levels(cut(seq.Date(Start, End, by = end_of),breaks =
>> every)))
>>   Vec[-1] - 1
>> }
>> qtrs = get_dates(from_date, to_date,end_of= "month",every = "quarter")
>> qtrs = qtrs[length(qtrs):1]
>> > qtrs
>>  [1] "2012-03-31" "2011-12-31" "2011-09-30" "2011-06-30" "2011-03-31"
>> "2010-12-31" "2010-09-30" "2010-06-30" "2010-03-31"
>>  [10] "2009-12-31" "2009-09-30" "2009-06-30" "2009-03-31" "2008-12-31"
>> "2008-09-30" "2008-06-30" "2008-03-31" "2007-12-31"
>> [19] "2007-09-30" "2007-06-30" "2007-03-31"
>>
>>
>> On Sat, Jun 30, 2012 at 1:10 PM, Jeffrey Ryan <jeffrey.ryan@lemnica.com>wrote:
>>
>>> You can look into ?endpoints in xts, but you haven't exactly provided
>>> a _reproducible_ example as requested in the posting guidelines.  Not
>>> personally sure what "flat matrix" is for instance.
>>>
>>> Also likely why no one has responded thus far.  If you make it _easy_
>>> to answer (simple examples which we can try and run), people tend to
>>> answer.
>>>
>>> Jeff
>>>
>>> On Thu, Jun 28, 2012 at 11:42 AM, Ben quant <ccquant@gmail.com> wrote:
>>> > Hello,
>>> >
>>> > I'm using the Rbbg package (please suggest an alternative that is
>>> faster if
>>> > you know of one). I'd like to get my data into a flat matrix where row
>>> > names are found like this for quarterly data:
>>> >
>>> > get_dates <- function(Start, End,end_of,every)
>>> > {
>>> >   Vec <- as.Date(levels(cut(seq.Date(Start, End, by = end_of),breaks =
>>> > every)))
>>> >   Vec[-1] - 1
>>> > }
>>> > qtrs = get_dates(from_date, to_date,end_of= "month",every = "quarter")
>>> > qtrs = qtrs[length(qtrs):1]
>>> >> qtrs
>>> > [1] "2012-03-31" "2011-12-31" "2011-09-30" "2011-06-30" "2011-03-31"
>>> > "2010-12-31" "2010-09-30" "2010-06-30" "2010-03-31" "2009-12-31"
>>> > [11] "2009-09-30" "2009-06-30" "2009-03-31" "2008-12-31" "2008-09-30"
>>> > "2008-06-30" "2008-03-31" "2007-12-31" "2007-09-30" "2007-06-30"
>>> >
>>> > ...and all companies that report after the quarter end date (above) and
>>> > before the next one are moved back one. For example, if a company
>>> reporting
>>> > period ended in Jan. 2012, those values would be on the 2011-12-31 row.
>>> > (These are then adjusted for look-ahead using the actual report date.)
>>> >
>>> > Is there a function that will do this in zoo or another package?
>>> >
>>> > Thanks!
>>> >
>>> > Ben
>>> >
>>> >         [[alternative HTML version deleted]]
>>> >
>>> > _______________________________________________
>>> > R-SIG-Finance@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.
>>>
>>>
>>>
>>> --
>>> Jeffrey Ryan
>>> jeffrey.ryan@lemnica.com
>>>
>>> www.lemnica.com
>>> www.esotericR.com
>>>
>>
>>
>

	[[alternative HTML version deleted]]

