R: Creating a dynamic list of xts objects

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

I try to loop through row vectors (each of them is an economical ticker) and create an xts object for each of them (length varies for each ticker) using the getSymbols function from the quantmod package. Then I want to make each xts object a single point data in the data frame. I also plan to have some related data in the data frame (for example, the maximum date in each xts object and the "header" that I specify elsewhere, etc.), but I can handle it myself.

Just trying to create a list of xts objects infuriates me. When I just try something like this, I always get 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")

xts . .

.

+4
2

, , . auto.assign = FALSE, . '[' '[['.

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"
+6

, R. , . , . tidyquant, . -, . tq_get() . . FRED (. ).

:

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

FRED:

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

. "getters", quantmod, . !

-1

All Articles