Pregunta

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       

¿Hay otra forma de obtener datos de formato correctos con Read.fwf o Read.table? ¿Puedo obtener el mismo resultado sin skip=1 en read.fwf o con read.table?

¿Fue útil?

Solución

Lea desde una tubería en lugar de de un archivo y deje que la tubería maneje las transformaciones que necesita, que pueden ser tan simples como grep -v '^$' para omitir líneas vacías.

los pipe() la función se describe en help(connections).

Aquí hay un ejemplo:

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> 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top