mks212
mks212

Reputation: 921

read.csv directly into character vector in R

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.

Upvotes: 2

Views: 3244

Answers (3)

bartektartanus
bartektartanus

Reputation: 16080

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.

Upvotes: 0

Jake Burkhead
Jake Burkhead

Reputation: 6535

SymbolList <- readLines("DJIA.csv")

Upvotes: 4

ALiX
ALiX

Reputation: 1031

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

Upvotes: 2

Related Questions