6
votes

I am trying to automate the reading in of files generated from another analysis program.

The standard output is typically in 6 columns separated by spaces with a carriage return at the end. This reads in nicely by simply using "strip.white = TRUE" in "read.table."

I'm having a problem, however, b/c an annotation is added to a line if a parameter was fixed as a constant.
Adding "flush = TRUE" allows me to skip over these occasional comments and read everything in.

What I would like to do is have these comments, which might only occur once in a given file, be added as a 7th column.

Is there read-in approach that allows me to specify the number of columns or some other way of accomadating this 7th column?

A snippet of data can be found here

The data look like this:

columns_1&2  column_3  column_4  column_6  column_6   column_7     
84:S 0:dorm  1.0000000 0.11E-005 0.9999979 1.0000021                           
85:p N:veg   1.0000000 0.0000000 1.0000000 1.0000000  Fixed               
86:p 0:dorm  0.260E-08 0.237E-05 -0.03E-05 0.46E-005
1

1 Answers

11
votes

If all of your columns are as neatly arranged as those in the linked example (i.e. if it is a "fixed-width file"), then this is a job for read.fwf():

df <- read.fwf(file = "http://dl.dropbox.com/u/54791824/SO_data_frag.txt", 
               widths = c(8, 7, 29, 15, 16, 16,1000))

head(df,4)
        V1      V2 V3           V4        V5       V6           V7
1    82:S  0:dorm   1 1.625420e-06 0.9999968 1.000003                      
2    83:S  0:dorm   1 1.083245e-06 0.9999979 1.000002                      
3    84:S  0:dorm   1 1.081771e-06 0.9999979 1.000002                      
4    85:p  N:veg    1 0.000000e+00 1.0000000 1.000000  Fixed   

EDIT: Alternatively, as Joran points out in comments, you could use read.table() with the fill=TRUE option:

df2 <- read.table(file = "http://dl.dropbox.com/u/54791824/SO_data_frag.txt", 
                  fill = TRUE, 
                   col.names=paste("column", 1:7, sep="_")