1

I am trying to read text header files in R. I have been pretty successful except for the following:

One part of the file contains:

.  
.    
. (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}  
.  
.  
.

I want the output to be a list containing the names of the spectra, i.e. ["Endmember 1" "Endmember 2" and so on...

I tried read.csv(), read.table(), but nothing seems to work. I am stumped by the ends of the lines and the '{', '}' characters

What I want to do is: start reading at "spectra names" and then store everything between the {} as an array of strings as I mentioned above.

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ステートメントを1行にまとめる方法があると確信しています。これがあなたが望むことを正確に行わない場合、多分あなたはそれを出発点として使うことができます。

列名のリストを作成する部分を理解したかどうかはわかりませんでした。

于 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 に答える