0
votes

I'm new to R and I am trying to store my tickers in a CSV file, having my codes read from the CSV and run through some codes,My CSV has only 3 lines:

Ticker

YHOO

IBM

below is my code.

Stocks <- read.csv(file.choose(),head=T) # tried to put in sep="," no luck
#loop through the tickers
for (i in Stocks){

getSymbols(i) 
# do something here
}

then i get this error Error in do.call(paste("getSymbols.", symbol.source, sep = ""), list(Symbols = current.symbols, : could not find function "getSymbols.1"

I try to debug, so i do

for (i in Stocks) {print (i)}

[1] YHOO IBM

Levels: IBM YHOO

this is what i get. i think when i am trying to run the loop the code is taking YHOO IBM as 1 ticker rather than loading 1 ticker at a time. Can someone advise what is the proper way of reading from a CSV file and loop through the ticker? appreciate the advise!

2
there's a really good chance your line breaks are problematic and we can't diagnose that with hand-typed "data" - hrbrmstr
Can you create the CSV file within the R code to make this reproducible? - Hack-R

2 Answers

2
votes

There is nothing wrong with your read statement nor your data.frame. The only problem is your for loop. You need:

 for (i in Stocks$Ticker) {print (i)}
[1] "YHOO"
[1] "IBM"
0
votes

getSymbols can take a vector as an input so there is no need for a loop, for example you could do something like:

Stocks <- read.csv(file.choose(),head=T)     

getSymbols(as.vector(Stocks$Ticker))