2015-05-02 68 views
0

我有一些.txt文件,標題大小不同。我只需要Column01到Column18中的數字。如何閱讀它們?來自Read in multiple .txt files with header in R的方法不適用於我。正在載入.txt文件,標題爲

POM01.DEF 


Timeplot 
Column01: V RMS L1 [V] 
Column02: V RMS L2 [V] 
Column03: V RMS L3 [V] 
Column04: I RMS L1 [A] 
Column05: I RMS L2 [A] 
Column06: I RMS L3 [A] 
Column07: P L1 [W] 
Column08: P L2 [W] 
Column09: P L3 [W] 
Column10: P Sum [W] 
Column11: Q L1 [VAr] 
Column12: Q L2 [VAr] 
Column13: Q L3 [VAr] 
Column14: Q Sum [VAr] 
Column15: PF L1 [1] 
Column16: PF L2 [1] 
Column17: PF L3 [1] 
Column18: PF Sum [1] 

Time       Column01 Column02 Column03 Column04 Column05 Column06 Column07 Column08 Column09 Column10 Column11 Column12 Column13 Column14 Column15 Column16 Column17 Column18 
18.03.2009 13:20:00    5762,2  5111,9  4338,7  197,20  214,69  219,80  0,43e+3 110,40e+3  0,00e+3 0,1108e+6 -1,73e+3 -70,50e+3  0,00e+3 -72,22e+3 -0,00038 -0,10060  0,00000 -0,03477 
18.03.2009 13:30:00    8988,6  9156,5  9125,4  19,86  40,60  14,53 46,63e+3 252,66e+3 25,93e+3 0,3252e+6 -41,32e+3 -245,37e+3 -22,70e+3 -309,39e+3 -0,26123 -0,67965 -0,19553 -0,47625 
+2

你可以使用'readlines方法/ read.table'即'線< - readlines方法( 'Kulis.txt'); IND < - grep的('\ \ d {2} \\。\\ d {2}。',lines)[1] -1; lines1 < - lines [ind:length(lines)]; lines1 [-1] < - gsub('^ |(?<=:\\ d {2})',「'」,lines1 [-1],perl = TRUE); read.table(text = lines1,header = TRUE,dec =「,」)' – akrun

回答

2

這類似於@akrun

wh <- grep('\\bTime\\b', rl <- readLines('~/desktop/tmp.txt')) 
rl <- gsub('\\s{2,}', ';', rl[wh:length(rl)]) 

read.table(text = rl, header = TRUE, sep = ';', dec = ',') 


#     Time Column01 Column02 Column03 Column04 Column05 Column06 Column07 Column08 Column09 Column10 Column11 Column12 Column13 Column14 Column15 Column16 Column17 Column18 
# 1 18.03.2009 13:20:00 5762.2 5111.9 4338.7 197.20 214.69 219.80  430 110400  0 110800 -1730 -70500  0 -72220 -0.00038 -0.10060 0.00000 -0.03477 
# 2 18.03.2009 13:30:00 8988.6 9156.5 9125.4 19.86 40.60 14.53 46630 252660 25930 325200 -41320 -245370 -22700 -309390 -0.26123 -0.67965 -0.19553 -0.47625