Frage

raw<-"                        
+ x y z w a s d f g h     
+ 1 2 3 4 5               
+ 1 2 3 4 5 6 7 8 9 10    
+     1 2             "    
raw    
[1] "                    \nx y z w a s d f g h \n1 2 3 4 5           \n1 2 3 4 5 6 7 8 9 10\n    1 2             "    
read.fwf(textConnection(raw),widths=c(rep(2,10)))    
  V1 V2 V3 V4 V5 V6 V7 V8 V9 V10    
1                                   
2 x  y  z  w  a  s  d  f  g   h     
3 1  2  3  4  5                     
4 1  2  3  4  5  6  7  8  9   10    
5       1  2                        
read.fwf(textConnection(raw),widths=c(rep(2,10)),skip=1)    
   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10    
1 x  y  z  w  a  s  d  f  g   h     
2 1  2  3  4  5                     
3 1  2  3  4  5  6  7  8  9   10    
4       1  2       

Gibt es eine andere Möglichkeit, Daten mit read.fwf oder read.table richtige Formatdaten zu erhalten? Kann ich das gleiche Ergebnis ohne bekommen? skip=1 in read.fwf oder mit read.table?

War es hilfreich?

Lösung

Lesen Sie aus einem Rohr statt aus einer Datei aus und lassen Sie das Rohr die von Ihnen benötigten Transformationen verarbeiten, die so einfach sein können wie grep -v '^$' Um leere Zeilen zu überspringen.

Das pipe() Funktion ist in beschrieben in help(connections).

Hier ist ein Beispiel:

R> read.fwf("/tmp/raw.txt", width=rep(2,10), skip=1)
  V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1 x  y  z  w  a  s  d  f  g   h 
2 1  2  3  4  5                 
3 1  2  3  4  5  6  7  8  9   10
4          1  2                 
R> read.fwf(pipe("grep -v '^$' /tmp/raw.txt"), width=c(rep(2,10)))
  V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1 x  y  z  w  a  s  d  f  g   h 
2 1  2  3  4  5                 
3 1  2  3  4  5  6  7  8  9   10
4          1  2                 
R> 
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top