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

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

-------------Problems Reply------------

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="_")

Category:r Views:0 Time:2011-12-28

Related post

  • Find the number of columns in a table 2009-03-18

    It is possible to find the number of rows in a table: select count(*) from tablename Is it possible to find the number of columns in a table? --------------Solutions------------- SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = 'd

  • Getting Error as "input array is longer than the number of columns in this table" 2010-02-28

    Code. Public Function comb1(ByVal SName As String) As DataTable Dim dt As New DataTable cmd = New SqlCommand("Select Distinct RName from tb_RS_New", con) dr2 = cmd.ExecuteReader While (dr2.Read()) dt.Rows.Add(dr2("RName")) End While Return dt End Fun

  • Input array is longer than the number of columns in this table. Exception 2010-07-05

    I'm using Following code to read CSV file contents. http://www.sendspace.com/file/l0zki3 link to download CSV file.. This is the CSV file which is located in my C:\ Drive. The exception I am getting is "Input array is longer than the number of column

  • Determine number of columns in a table in Word using VBA 2010-08-01

    just a quick question. I've got this table in a Word template which have two columns by default. Then I've got this button the user can press if he wants another column. The macro run inserts several text placeholders and formats certain things autom

  • How to count number of columns in a table in SQLITE? 2010-11-29

    How can I count the number of columns in a table in a sqlite database in Android? --------------Solutions------------- A query http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#query returns a Cursor which have method

  • Input array is longer than the number of columns in this table 2011-02-22

    I've recently started to use SQLite and began to integrate it into a C# project I'm working on. However, randomly my project will throw the exception: Input array is longer than the number of columns in this table I'm having a hard time trying the tr

  • SQL: Count number of columns in all tables, excluding views 2011-03-01

    I am creating a query that returns the number of columns in each table, but I want to exclude Views. The following works but returns View results: SELECT COUNT(*), table_name FROM INFORMATION_SCHEMA.COLUMNS Group By table_name Any suggestions? NOTE:

  • Number of column in a table MySQL 2011-10-02

    Possible Duplicate: Find the number of columns in a table I would like to know what is the query which enables to count the number of columns in a table in MySQL. Something like : SELECT COUNTNUMBEROFCOLUMN(*) FROM mytable --------------Solutions----

  • How do you count the number of columns in a table 2011-10-29

    How to find the number of columns in a table. desc foo (actually mentions the list of columns at the end of the table, as the number of rows it found), but is there a different way to find it? --------------Solutions------------- Take a look at this

  • "Input array is longer than the number of columns in this table" in c# application 2012-01-14

    i have windows application in which i am constructing dataset with two datatables one is "Products" and other is "TaxView".I have included sample code below.in this code i am getting error Input array is longer than the number of columns in this tabl

  • How to count the number of columns in a table using SQL? 2012-04-10

    This question already has an answer here: Number of columns 5 answers How to count the number of columns in a table using SQL? I am using Oracle 11g Please help. t. --------------Solutions------------- select count(*) from user_tab_columns where tabl

  • To get total number of columns in a table in sql 2009-09-23

    I need a query in sql to get total columns in a table.Can anybody help? --------------Solutions------------- SELECT COUNT(COLUMN_NAME) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_CATALOG = 'database' AND TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'table'

  • What are ways to reduce the number of columns in a table/grid? 2010-02-05

    I have a datagrid with many columns. This makes it pretty wide. Now we want to add more information to the table. Aside from removing or shortening existing columns what are some ways we might be able to add additional information without adding new

  • Calculating the optimal number of columns for a table layout - Only given the table width and a list of rectangles 2011-03-02

    I have a list of rectangles with different dimensions. rects = [100x20, 30x10, 10x10, 70x20, 40x30, 50x10] I am trying to render a table from these rectangles. If I would have a fix number of columns, I simply could calculate the number of rows and t

  • How to change the number of columns in a table dynamically but still keep the table layout intact? 2010-05-07

    I have a 'n' column table , the first column is a drop down box , on selecting the options from the drop down, the other columns change. For eg: first drop down might need to display a text box and a button , the second drop down 3 text boxes and a b

  • Setting number to column from counter table in same transaction 2011-11-22

    I have a table where I keep a counter acording to a "document_type" TABLE: document_counter document_type = "general" last_number = 100 TABLE: document document_id document_number document_type description ... What is the correct way to handle the tr

  • Javascript: Count number of columns in a table row 2012-04-06

    I have a table similar to <table id="table1"> <tr> <td><input type="text" value="" /></td> <td><input type="text" value="" /></td> <td><input type="text" value="" /></td> <td>

  • PHP find average of number from column on associated table 2014-09-19

    It is possible I have mis-tited this question. I have two tables, Users and Reviews. Stars is a column in the Reviews table. I just want to be able to show what the User's average star rating is. I can't figure out how to access these then find the a

  • Does the number of columns returned affect the speed of a query? 2009-05-12

    If I have two queries SELECT Id, Forename, Surname FROM Person WHERE PersonName Like(‘%frank%’) And SELECT * FROM Person WHERE PersonName Like(‘%frank%’) Which query will run faster? Is the where clause / table joining the biggest factor, or the numb

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.116 (s). 11 q(s)