简体   繁体   English

将read.csv直接转换为R中的字符向量

[英]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. 我有一个CSV文件,其中有一栏股票代码。 I then read this csv into R and apply functions to each ticker using a for loop. 然后,我将此csv读入R,并使用for循环将函数应用于每个行情收录器。

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. 我读了csv,然后进入数据框并拉出for循环需要正确运行的字符向量。

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? 也许read.csv不是最好的命令?

Thank you. 谢谢。

UPDATE UPDATE

I am using the readlines method suggested by Jake and Bartek. 我正在使用Jake和Bartek建议的readlines方法。 There is a warning "incomplete final line found on" the csv file but I ignore it since the data is correct. 在csv文件中有一个警告“最后一行不完整”,但是由于数据正确,我将其忽略。

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

readLines function is the best solution here. readLines函数是此处的最佳解决方案。 Please note that read.csv function is not only for reading files with csv extensions. 请注意,read.csv函数不仅用于读取带有csv扩展名的文件。 This is simply read.table function with parameters like header or sep set differently. 这只是具有不同参数(如header或sep设置)的read.table函数。 Check the documentation for more info. 查看文档以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM