[R-SIG-Finance] create a new variable in a loop - Rbbg, RBloomberg

Jeffrey Ryan jeff.a.ryan at gmail.com
Thu Jun 14 20:42:17 CEST 2012


getSymbols follows the R paradigm of load(), albeit possibly confusingly
named.  I.e. Load data from an external source into an environment.  The
getSymbols call also includes an envir= argument that defaults to
GlobalEnv, but is settable to any environment object.

Environments are far more useful in R than most people recognize, since
they are _never_ copied - meaning lots of objects in a 'container' don't
need to be copied when used for arguments, etc.

You can of of course use lists, but when dealing with many symbols,
new.env(hash=TRUE) is a very memory and search friendly [O(1) symbol
lookup] option that should really be used far more often than it is in
typical R code/examples.

Jeff

On 6/14/12 12:09 PM, "John Laing" <john.laing at gmail.com> wrote:

>Marco,
>
>I would recommend against this approach of creating and renaming many
>environment variables in a loop. Some packages (e.g.
>quantmod::getSymbols) do exactly that, but I prefer to use a list.
>This feels more consistent with the R idiom of applying a function to
>a vector:
>
>securities <- c("SPGLOB Index", "SPX Index", "SPE Index", "SPTPX
>Index", "SPTSX60 Index", "AS31 Index", "SPA50 Index", "SPLAC Index")
>fields <- c("PX_OPEN", "PX_HIGH", "PX_LOW", "PX_LAST", "PX_VOLUME")
>startdate <- as.Date("2005-11-16")
>enddate <- as.Date(Sys.Date())
>
>bbg2xts <- function(security, ...) {
>    bbg <- bdh(conn, security, ...)
>    bbg$date <- NULL
>    as.xts(bbg, order.by = as.Date(rownames(bbg)))
>}
>
>BBGData <- lapply(securities, bbg2xts, fields, startdate, enddate,
>    option_names = c("nonTradingDayFillOption",
>"nonTradingDayFillMethod"),
>    option_values = c("NON_TRADING_WEEKDAYS", "PREVIOUS_VALUE"))
>names(BBGData) <- securities
>
>You get back is a list of XTS objects, with elements named appropriately.
>
>-John
>
>On Thu, Jun 14, 2012 at 10:35 AM, Belgarath <marco.cora at googlemail.com>
>wrote:
>> Hello Everyone,
>>
>> I have the following code:
>>
>> -----
>> securities <- c("SPGLOB", "SPX Index", "SPE Index", "SPTPX Index",
>>            "SPTSX60 Index", "AS31 Index", "SPA50 Index", "SPLAC Index")
>> fields <- c("PX_OPEN", "PX_HIGH", "PX_LOW", "PX_LAST", "PX_VOLUME")
>> startdate <- as.Date("2005-11-16")
>> enddate <- as.Date(Sys.Date())
>> BBGData <- bdh(conn, paste(securities[1],"Index",sep=" "),
>>               fields, startdate, enddate,
>>               option_names = c("nonTradingDayFillOption",
>> "nonTradingDayFillMethod"),
>>               option_values = c("NON_TRADING_WEEKDAYS",
>>"PREVIOUS_VALUE"))
>> BBGData$date=NULL
>> BBGData2 <- as.xts(BBGData, order.by =as.Date(rownames(BBGData)))
>>
>> ----
>>
>> I would like to assign BBGData2 to a new variable with the name as
>> securities[1] in order to be able to package it in a loop and download
>>all
>> the info for all the securities and create new variables for each.
>>
>> More generally how do I create new r variables in a Loop, sort of:
>>
>> name(securities[1]) <- BBGData2
>>
>> Alternatively how do I change a variable name, how to change the name of
>> BBGData2 into SPGLOB for instance; at the next run of the loop the
>>BBGData2
>> would be recreated anyway.
>>
>> Thank you.
>> Regards,
>> Marco
>>
>>
>>
>> --
>> View this message in context:
>>http://r.789695.n4.nabble.com/create-a-new-variable-in-a-loop-Rbbg-RBloom
>>berg-tp4633384.html
>> Sent from the Rmetrics mailing list archive at Nabble.com.
>>
>> _______________________________________________
>> 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.
>
>_______________________________________________
>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