0

Assume we have a list of characters as basis for a function, which has a xts object with the same name as result:

library(zoo)
library(xts)
library(quantmod)

l<-list("AAPL","NKE")

for(i in 1:length(l)){
  getSymbols(l[[i]], src = "yahoo")
  write.zoo(l[[i]], paste(l[[i]],".csv", sep=''), sep = ",")
}

My code does not work, cause getSymbols creates an xts object (named AAPL / NKE). My problem is, that I cannot call them properly in the write.zoo function. Can you please help me?

Joe
  • 15
  • 6
  • To clarify, are you asking how to write the contents of the xts objects with `write.zoo`? When you say that you cannot call them properly, what are you seeing that indicates that? – Don Rowe Feb 11 '20 at 00:40

2 Answers2

2

Call getSymbols with auto = FALSE to get the data directly.

library(quantmod)

syms <- c("AAPL", "NKE")
for(s in syms) {
  dat <- getSymbols(s, auto = FALSE)
  write.zoo(dat, paste0(s, ".csv"), sep = ",")
}
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
0

Here, we need get to get the value of the object created

for(i in 1:length(l)){
  getSymbols(l[[i]], src = "yahoo")
   write.zoo(get(l[[i]]), paste(l[[i]],".csv", sep=''), sep = ",")
  }

-checking

enter image description here

akrun
  • 874,273
  • 37
  • 540
  • 662