我有1500万个CSV文件,每个文件有两列(整数和浮点数),以及5到500行.每个文件看起来像:
3453,0.034
31,0.031
567,0.456
...
Run Code Online (Sandbox Code Playgroud)
目前,我正在迭代所有文件,并使用read.csv()
将每个文件导入大列表.这是一个简化版本:
allFileNames = Sys.glob(sprintf("%s/*/*/results/*/*", dir))
s$scores = list()
for (i in 1:length(allFileNames)){
if ((i %% 1000) == 0){
cat(sprintf("%d of %d\n", i, length(allFileNames)))
}
fileName = allFileNames[i]
approachID = getApproachID(fileName)
bugID = getBugID(fileName)
size = file.info(fileName)$size
if (!is.na(size) && size > 0){ # make sure file exists and is not empty
tmp = read.csv(fileName, header=F, colClasses=c("integer", "numeric"))
colnames(tmp) = c("fileCode", "score")
s$scores[[approachID]][[bugID]] = tmp
} else {
# File does not exist, or is empty.
s$scores[[approachID]][[bugID]] = matrix(-1, ncol=2, nrow=1)
}
}
tmp = read.csv(fileName, header=F, colClasses=c("integer", "numeric")
Run Code Online (Sandbox Code Playgroud)
稍后在我的代码中,我将回顾列表中的每个矩阵,并计算一些指标.
启动此导入过程后,看起来需要3到5天才能完成.有更快的方法吗?
编辑:我添加了有关我的代码的更多详细信息.
我不清楚你的目标,但如果你试图将所有这些文件读入一个R数据结构,那么我会看到两个主要的性能问题:
所以做一些快速分析,看看读取的时间.如果他们在阅读更多文件时逐渐放慢速度,那么让我们关注问题#2.如果它一直很慢,那么让我们担心问题#1.
关于解决方案,我想你可以从两件事开始:
scan()
或readlines()
功能来优化它而不必偏离另一种语言.添加一些代码的更多细节(列表看起来像你正在使用的那些?),我们可能会更有帮助.
使用scan
(作为评论中的Joshua状态)可能更快(3-4次):
scan(fileName, what=list(0L,0.0), sep=",", dec=".", quiet=TRUE)
Run Code Online (Sandbox Code Playgroud)
主要区别在于scan
返回列表包含两个元素并read.csv
返回data.frame
.
归档时间: |
|
查看次数: |
2976 次 |
最近记录: |