需要读取没有很好格式的文本文件的特定行到数据帧(最好是多个数据帧)?

问题描述 投票:1回答:1

我对R来说是全新的,我不确定处理这个文件的最佳方式,所以我真的希望有人能够至少指出我正确的方向。我已经搜索了其他解决方案,并尝试使用grepl,但似乎无法找出只读取一些数据的最佳方法。我正在尝试阅读的文件看起来像下面的文字:

##BLOCKS= 8          
Plate:  Plate01 1.3 PlateFormat Endpoint    Absorbance  Raw FALSE   1               1   630 1   12  96  1   8   None    
Temperature(°C) 1       2       3       4       5       6       7       8       9       10      11      12      
0.00            0.042   0.067   0.292   0.206   0.071   0.067   0.04    0.063   0.059   0.04    0.066   0.04        
                0.043   0.172   0.179   0.199   0.073   0.067   0.04    0.062   0.058   0.039   0.066   0.039       
                 0.04   0.066   0.29    0.185   0.072   0.067   0.04    0.062   0.058   0.039   0.065   0.039       
                0.039   0.068   0.291   0.189   0.075   0.069   0.04    0.064   0.058   0.041   0.064   0.039       
                0.042   0.063   0.271   0.191   0.07    0.068   0.04    0.065   0.058   0.041   0.066   0.04        
                0.041   0.067   0.342   0.199   0.069   0.066   0.041   0.065   0.057   0.04    0.065   0.042       
                0.044   0.064   0.295   0.198   0.069   0.067   0.039   0.064   0.057   0.04    0.067   0.041       
                0.041   0.067   0.29    0.211   0.066   0.067   0.043   0.056   0.058   0.042   0.067   0.042       

~End
Plate:  Plate#1 1.3 PlateFormat Endpoint    Absorbance  Raw FALSE   1                       1   630 1   12  96  1   8   None    
Temperature(°C) 1       2       3       4       5       6       7       8       9       10      11      12      
0.00            0.042   0.072   0.257   0.165   0.074   0.07    0.04    0.067   0.055   0.04    0.07    0.04        
                0.042   0.164   0.136   0.195   0.075   0.07    0.041   0.066   0.055   0.04    0.069   0.04        
                0.041   0.07    0.344   0.198   0.074   0.069   0.041   0.065   0.055   0.04    0.068   0.04        
                0.04    0.069   0.307   0.199   0.075   0.072   0.041   0.067   0.055   0.043   0.068   0.041       
                0.043   0.068   0.296   0.214   0.072   0.071   0.042   0.067   0.055   0.041   0.068   0.041       
                0.041   0.071   0.452   0.241   0.072   0.069   0.042   0.067   0.054   0.041   0.068   0.043       
                0.044   0.068   0.299   0.182   0.071   0.071   0.042   0.067   0.054   0.041   0.069   0.041       
                0.042   0.071   0.333   0.13    0.068   0.07    0.042   0.058   0.054   0.042   0.07    0.041       

~End

我只想要编号为1-12(温度旁边)的列/行及其下的数据。我是R的新手,但确实有一些编程经验,所以我不一定需要有人告诉我具体如何做到这一点,但如果有人能够至少指出我正确的方向我应该看到的任何功能我'我真的很感激帮助!

r
1个回答
2
投票

第1步:使用readLines将数据导入R会话

Lines <- readLines(textConnection("##BLOCKS= 8          
Plate:  Plate01 1.3 PlateFormat Endpoint    Absorbance  Raw FALSE   1               1   630 1   12  96  1   8   None    
Temperature(°C) 1       2       3       4       5       6       7       8       9       10      11      12      
0.00            0.042   0.067   0.292   0.206   0.071   0.067   0.04    0.063   0.059   0.04    0.066   0.04        
                0.043   0.172   0.179   0.199   0.073   0.067   0.04    0.062   0.058   0.039   0.066   0.039       
                 0.04   0.066   0.29    0.185   0.072   0.067   0.04    0.062   0.058   0.039   0.065   0.039       
                0.039   0.068   0.291   0.189   0.075   0.069   0.04    0.064   0.058   0.041   0.064   0.039       
                0.042   0.063   0.271   0.191   0.07    0.068   0.04    0.065   0.058   0.041   0.066   0.04        
                0.041   0.067   0.342   0.199   0.069   0.066   0.041   0.065   0.057   0.04    0.065   0.042       
                0.044   0.064   0.295   0.198   0.069   0.067   0.039   0.064   0.057   0.04    0.067   0.041       
                0.041   0.067   0.29    0.211   0.066   0.067   0.043   0.056   0.058   0.042   0.067   0.042       

~End
Plate:  Plate#1 1.3 PlateFormat Endpoint    Absorbance  Raw FALSE   1                       1   630 1   12  96  1   8   None    
Temperature(°C) 1       2       3       4       5       6       7       8       9       10      11      12      
0.00            0.042   0.072   0.257   0.165   0.074   0.07    0.04    0.067   0.055   0.04    0.07    0.04        
                0.042   0.164   0.136   0.195   0.075   0.07    0.041   0.066   0.055   0.04    0.069   0.04        
                0.041   0.07    0.344   0.198   0.074   0.069   0.041   0.065   0.055   0.04    0.068   0.04        
                0.04    0.069   0.307   0.199   0.075   0.072   0.041   0.067   0.055   0.043   0.068   0.041       
                0.043   0.068   0.296   0.214   0.072   0.071   0.042   0.067   0.055   0.041   0.068   0.041       
                0.041   0.071   0.452   0.241   0.072   0.069   0.042   0.067   0.054   0.041   0.068   0.043       
                0.044   0.068   0.299   0.182   0.071   0.071   0.042   0.067   0.054   0.041   0.069   0.041       
                0.042   0.071   0.333   0.13    0.068   0.07    0.042   0.058   0.054   0.042   0.07    0.041       

~End"))

步骤2和3:构建条件以包括良好的数据线和分组

?strsplit 
# Couldn't remember name of `substr`, figured the ?strsplit  page would show link

start <- substr(Lines, 1,1)  # 1st char was sufficient to build a rule
table(start)
#--- result ----
start
       #  ~  0  P  T      # the 14 is the count of " " (just spaces)
 2 14  1  2  2  2  2 
#end table
goodL <- Lines[start %in% c(" ","T","0")  ]
goodL  # Look at result

group <- cumsum(substr(goodL , 1,4)=="Temp")  #build grouping
group   # check the grouping variable
 [1] 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2

第4步:使用lapply(split(goodL, group), function(x) ...处理组

dfrms <- lapply(split(goodL, group), 
             function(x) read.table(text=substr(x,16,  # stuff to right of 16th char
                                                     100),header=TRUE))
str(dfrms)  # check result,,, not correct, need 12th entry
List of 2
 $ 1:'data.frame':  8 obs. of  11 variables:
  ..$ X1 : num [1:8] 0.042 0.043 0.04 0.039 0.042 0.041 0.044 0.041
  ..$ X2 : num [1:8] 0.067 0.172 0.066 0.068 0.063 0.067 0.064 0.067
 #   -----snipped output

 dfrms <- lapply(split(goodL, group),   # will be a list of dataframes
            function(x) read.table(text =substr(x, 16, 120), header=TRUE))
 str(dfrms)   # Looks good
List of 2
 $ 1:'data.frame':  8 obs. of  12 variables:
  ..$ X1 : num [1:8] 0.042 0.043 0.04 0.039 0.042 0.041 0.044 0.041
  ..$ X2 : num [1:8] 0.067 0.172 0.066 0.068 0.063 0.067 0.064 0.067
  ..$ X3 : num [1:8] 0.292 0.179 0.29 0.291 0.271 0.342 0.295 0.29
 #--- snippped output

我想赞扬@ G.Grothendieck这个策略。对“user:516548 readLines”进行搜索将获得许多其他类似方法的优雅示例。

© www.soinside.com 2019 - 2024. All rights reserved.