4

Going crazy trying to create some type of list/data frame containing xts objects.

I'm trying loop through a vector of strings (each an economic "ticker"), and create an xts object for each (length varies for each "ticker") using the getSymbols function from the quantmod package. Then I want to make each xts object one data point in a data frame. I also plan to have some associated data in the data frame (like, the maximum date in each xts object, and a "title" that I specify elsewhere, etc.), but I can handle that myself.

Just trying to create a list of xts objects is driving me nuts. When I just try something like this, I always wind up getting a list of strings:

test <- list()

for (i in 1:length(fredTickers))
{# import Data from FRED database
    # this creates a list of strings, I'm hoping for list of xts objects...
    test[i] <- getSymbols(fredTickers[i],src="FRED")
    # xts objects are created for each, but not assigned to the list
}

# this creates an xts object named EVANQ. 
# The test2 object is just a string with value "EVANQ".
test2 <- getSymbols("EVANQ",src="FRED")

Handling these xts objects is driving me nuts. I've tried many tricks.

Thank you for your help.

2 Answers 2

8

It is stated multiple times in the documentation that by default the object is assigned to the global environment, not returned explicitly. Specify auto.assign=FALSE to do the opposite. Also recall the difference between '[' and '[['.

tickers <- c("F", "YHOO")
test <- list()

for (i in 1:length(tickers)) {
  test[[i]] <- getSymbols(tickers[i], src="yahoo", auto.assign=FALSE, return.class="xts")
}

head(test[[1]])
           F.Open F.High F.Low F.Close F.Volume F.Adjusted
2007-01-03   7.56   7.67  7.44    7.51 78652200       7.18
2007-01-04   7.56   7.72  7.43    7.70 63454900       7.36
2007-01-05   7.72   7.75  7.57    7.62 40562100       7.29
2007-01-08   7.63   7.75  7.62    7.73 48938500       7.39
2007-01-09   7.75   7.86  7.73    7.79 56732200       7.45
2007-01-10   7.79   7.79  7.67    7.73 42397100       7.39

class(test[[1]])
[1] "xts" "zoo"
0
-1

Unfortunately you can't use strings with a matrix in R. The matrix data structure is homogeneous so the entire data must be the same class. You need to use a data frame for this task, and you're in luck. You might try the tidyquant package which takes care multiple assets. It doesn't require for-loops so it will save you a significant amount of code. The tq_get() function is responsible for getting stock prices. Here's a quick example with stock prices. You can do the same thing with FRED symbols (see below).

Getting stock prices:

library(tidyquant)

# Get some stock prices for multiple stocks
c("FB", "GOOG", "AMZN", "NFLX") %>%
    tq_get(get = "stock.prices", 
           from = "2013-01-01",
           to   = "2016-12-31")
#> # A tibble: 4,032 × 8
#>    symbol       date  open  high   low close    volume adjusted
#>     <chr>     <date> <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
#> 1      FB 2013-01-02 27.44 28.18 27.42 28.00  69846400    28.00
#> 2      FB 2013-01-03 27.88 28.47 27.59 27.77  63140600    27.77
#> 3      FB 2013-01-04 28.01 28.93 27.83 28.76  72715400    28.76
#> 4      FB 2013-01-07 28.69 29.79 28.65 29.42  83781800    29.42
#> 5      FB 2013-01-08 29.51 29.60 28.86 29.06  45871300    29.06
#> 6      FB 2013-01-09 29.67 30.60 29.49 30.59 104787700    30.59
#> 7      FB 2013-01-10 30.60 31.45 30.28 31.30  95316400    31.30
#> 8      FB 2013-01-11 31.28 31.96 31.10 31.72  89598000    31.72
#> 9      FB 2013-01-14 32.08 32.21 30.62 30.95  98892800    30.95
#> 10     FB 2013-01-15 30.64 31.71 29.88 30.10 173242600    30.10
#> # ... with 4,022 more rows

Getting FRED data:

library(tidyquant)

# Get some economic data for multiple FRED codes
c("CPIAUCSL", "A191RL1Q225SBEA", "IC4WSA") %>%
    tq_get(get = "economic.data")
#> # A tibble: 691 × 3
#>      symbol       date   price
#>       <chr>     <date>   <dbl>
#> 1  CPIAUCSL 2007-01-01 203.437
#> 2  CPIAUCSL 2007-02-01 204.226
#> 3  CPIAUCSL 2007-03-01 205.288
#> 4  CPIAUCSL 2007-04-01 205.904
#> 5  CPIAUCSL 2007-05-01 206.755
#> 6  CPIAUCSL 2007-06-01 207.234
#> 7  CPIAUCSL 2007-07-01 207.603
#> 8  CPIAUCSL 2007-08-01 207.667
#> 9  CPIAUCSL 2007-09-01 208.547
#> 10 CPIAUCSL 2007-10-01 209.190
#> # ... with 681 more rows

There's a bunch of options to choose from. I call them "getters", and you have most of the quantmod data sources that you can choose from. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.