在R中加速read.table?

时间:2012-10-25 22:40:34

标签: r

  

可能重复:
  Quickly reading very large tables as dataframes in R

我需要从一个大的(超过50MB)文本文件中重复读取不同的数据部分。目前,我的代码正在运行,但完成1次迭代需要30秒。任何人都可以帮助我提高代码的效率吗?

我的代码:

Skip_s=c(88334, 92244, 92635, 96154, 96545, 100455, 100846, 104365, 104756, 
         112967, 123524, 134081, 145929, 156877, 170171, 183856, 194804, 206143,
         217482, 230385, 245243, 255800)   #starting number of rows

nrows_s=c(380) #the length of code I will read
for (k in 1:length(Skip_s)){
    bb = read.table("file",skip=Skip_s[k]-1, nrows=nrows_s, colClasses = c("character",rep("numeric",6)))
    #I use read.table is because it can break the loaded information into columns
    TOT_s[k,1,Ite] = mean(bb$V4[1:105])  
    TOT_s[]..... #I have 20 this statements 
}

上述代码的完整版本需要30秒。大部分时间都花在read.table上。我在想有没有办法优化它?

以下是read.table函数

读取的数据示例
 CNC HORIZON     COMPARTMENT      TOTAL      ADSORBED   DISSOLVED  GAS CONC.
 CNC                              (MG/KG)    (MG/KG)    (MG/L)     (MG/L)
 CNC --------------------------------------------------------------------------
 CNC
 CNC
 CNC   1            1             0.4062     0.3737      1.210     0.2419E-05
 CNC   1            2             0.4942     0.4547      1.472     0.2943E-05
 CNC   1            3             0.4930     0.4536      1.468     0.2936E-05
 CNC   1            4             0.4812     0.4427      1.433     0.2865E-05
 CNC   1            5             0.4682     0.4307      1.394     0.2788E-05
 CNC   1            6             0.4550     0.4186      1.355     0.2710E-05
 CNC   1            7             0.4418     0.4065      1.315     0.2631E-05
 CNC   1            8             0.4286     0.3944      1.276     0.2552E-05
 CNC   1            9             0.4154     0.3822      1.237     0.2474E-05
 CNC   1           10             0.4022     0.3701      1.198     0.2395E-05

0 个答案:

没有答案