1

我正在尝试读取 R 中的文本头文件。除了以下内容,我已经非常成功:

该文件的一部分包含:

.  
.    
. (Other stuff)  
spectra names = {  
 Endmember  1, Endmember  2, Endmember  3, Endmember  4, Endmember  5,   
 Endmember  6, Endmember  7, Endmember  8, Endmember  9, Endmember  10,   
 Endmember  11, Endmember  12, Endmember  13, Endmember  14, Endmember  15,   
 Endmember  16, Endmember  17, Endmember  18, Endmember  19, Endmember  20,   
 Endmember  21, Endmember  22, Endmember  23, Endmember  24, Endmember  25,   
 Endmember  26, Endmember  27, Endmember  28, Endmember  29, Endmember  30}  
.  
.  
.

我希望输出是一个包含光谱名称的列表,即["Endmember 1" "Endmember 2"等等......

我试过了read.csv()read.table()但似乎没有任何效果。我被行尾和'{','}'字符难住了

我想做的是:从“光谱名称”开始阅读,然后将两者之间的所有内容存储{}为字符串数组,如上所述。

4

2 回答 2

1

这是如何实施贾斯汀建议的示例。假设数据文件如下所示:

4 5 6
2 3 4
1 2 3 
spectra names = {Endmember  1, Endmember  2, Endmember  3}
1 1 1
1 1 2
1 2 1
1 2 2
2 1 1
2 1 2
2 2 1
2 2 2

你可以这样读:

aa <- readLines('c:/users/Mark W Miller/simple R programs/odd data file with headers in the middle.txt', 
     n = 4)

bb <- gsub("spectra names = {","", aa[4], fixed=TRUE)
bb <- gsub("}","", bb, fixed=TRUE)
bb <- gsub("  "," ", bb, fixed=TRUE)
bb <- gsub(", ",",", bb, fixed=TRUE)
cc <- unlist(strsplit(bb, ","))
cc

dd <- read.table('c:/users/Mark W Miller/simple R programs/odd data file with headers in the middle.txt', 
     skip = 4, header=F)
dd

colnames(dd) <- cc
dd

我确信有办法将我的所有gsub陈述浓缩为一行。如果这不完全符合您的要求,也许您可​​以将其用作开始。

我不确定我是否理解为列名创建列表的部分。

于 2013-01-02T17:39:12.730 回答
1

按照评论中的建议使用 scan 和 gsub ,您可以轻松获得此功能。

tt <- scan(text = "spectra names = {  
  Endmember  1, Endmember  2, Endmember  3, Endmember  4, Endmember  5,   
  Endmember  6, Endmember  7, Endmember  8, Endmember  9, Endmember  10,   
  Endmember  11, Endmember  12, Endmember  13, Endmember  14, Endmember  15,   
  Endmember  16, Endmember  17, Endmember  18, Endmember  19, Endmember  20,   
  Endmember  21, Endmember  22, Endmember  23, Endmember  24, Endmember  25,   
  Endmember  26, Endmember  27, Endmember  28, Endmember  29, Endmember  30}" ,what = 'character' ,sep =',')

gsub('[{|}]','',tt) ## not really clever but it is a good begining
[1] "spectra names =   " "  Endmember  1"     " Endmember  2"      " Endmember  3"      " Endmember  4"      " Endmember  5"     
[7] "   "                "  Endmember  6"     " Endmember  7"      " Endmember  8"      " Endmember  9"      " Endmember  10"    
[13] "   "                "  Endmember  11"    " Endmember  12"     " Endmember  13"     " Endmember  14"     " Endmember  15"    
[19] "   "                "  Endmember  16"    " Endmember  17"     " Endmember  18"     " Endmember  19"     " Endmember  20"    
[25] "   "                "  Endmember  21"    " Endmember  22"     " Endmember  23"     " Endmember  24"     " Endmember  25"    
[31] "   "                "  Endmember  26"    " Endmember  27"     " Endmember  28"     " Endmember  29"     " Endmember  30"   
于 2013-01-02T17:41:12.240 回答