2016-06-06 70 views
1

我有以下格式的文本文件,我对两列数字的中间部分感兴趣。如何通过R读取此文本文件。如何在R中的文本文件中提取数字

Correct for Electrical Dark: No (NI25D137) 
Strobe/Lamp Enabled: No (NI25D137) 
Correct for Detector Non-linearity: No (NI25D137) 
Correct for Stray Light: No (NI25D137) 
Number of Pixels in Processed Spectrum: 256 
Begin Processed Spectral Data 
857.97 0.000 
864.83 7.252 
871.70 7.252 
878.56 7.155 
885.42 7.131 
892.27 7.113 
End Processed Spectral Data 
+0

您的数据实际上是在每行之间有行,还是在这里粘贴的结果呢? – thelatemail

+0

不,它没有行之间 –

回答

2

您可以使用readLines逐行读取。然后,使用一些正则表达式可以保留数字行。在使用read.table将已清除的文本转换为data.frame之前,我也将开头删除多余的空格。

ll <- readLines(con = textConnection("COPY YOUR TEXT HERE")) 
read.table(text=gsub("^ +","",grep("\\d+[.]\\d+ +\\d+[.]\\d+",ll,value=TRUE))) 

    V1 V2 
1 857.97 0.000 
2 864.83 7.252 
3 871.70 7.252 
4 878.56 7.155 
5 885.42 7.131 
6 892.27 7.113 
+0

有没有什么办法可以通过直接访问.txt文件来做到这一点? –

+0

@PrabeshJoshi当然是。您只需用文件名替换con = ...。 'readlines方法(path_filename)'。 – agstudy

相关问题