Can you specify the number of columns in read.table?

I am trying to automate reading files created from another analysis program.

The standard output is usually found in 6 columns, separated by spaces with a carriage return at the end. It reads well just using "strip.white = TRUE" in "read.table."

I have a problem, however, b / c added annotation to the string if the parameter was fixed as a constant.
Adding "flush = TRUE" allows me to skip these random comments and read everything.

What I would like to do is these comments, which can occur only once in this file, are added as the 7th column.

Is there a reading approach that allows me to specify the number of columns or some other way that fits this 7th column?

Below is a snippet of data here

The data is as follows:

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
+5
source share
1 answer

If all your columns are arranged as neatly as in the linked example (i.e. if it is a file with a fixed width), then this is the task 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 Goran points out in the comments, you can use read.table()with the option fill=TRUE:

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

All Articles