Question

This code works, however, I wonder if there is a more efficient way. I have a CSV file that has a single column of ticker symbols. I then read this csv into R and apply functions to each ticker using a for loop.

I read in the csv, and then go into the data frame and pull out the character vector that the for loop needs to run properly.

SymbolListDataFrame = read.csv("DJIA.csv", header = FALSE, stringsAsFactors=F)
SymbolList = SymbolListDataFrame[[1]]

for (Symbol in SymbolList){...}

Is there a way to combine the first two lines I have written into one? Maybe read.csv is not the best command for this?

Thank you.

UPDATE

I am using the readlines method suggested by Jake and Bartek. There is a warning "incomplete final line found on" the csv file but I ignore it since the data is correct.

Was it helpful?

Solution

SymbolList <- readLines("DJIA.csv")

OTHER TIPS

SymbolList <- read.csv("DJIA.csv", header = FALSE, stringsAsFactors=F)[[1]]

readLines function is the best solution here. Please note that read.csv function is not only for reading files with csv extensions. This is simply read.table function with parameters like header or sep set differently. Check the documentation for more info.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top