[R] skipping columns with read.fwf?

asone@latte.harvard.edu asone at latte.harvard.edu
Sat May 4 05:43:59 CEST 2002

While at least two people had already responded to your question and
probably you may have received other tips on this topic personally, this
e-mail intends to have two of these other tips get into the help archive
for future reference.

1. cut command and read.fwf

This is a once-and-for-all (preprocessing the file before R) approach:
if you handle the file on some Linux/Unix-type platform, you can extract
the necessary columns from the data file and save them as a new file by
using 'cut' command with -b option and '> a_new_file_name' [redirection]
; for MS windows, cut.exe can be found within the tools.zip available at

2. customize read.fwf

As Professor Ripley mentioned in his e-mail, read.fwf isn't suitable for
large files; however, if you stick to solutions within R, one approach
is to customize read.fwf that is just a wrapping function of scan,
read.table, and others. As you can see, read.fwf internally calculates
first and last positions to cut/save relevant columns [i.e., an internal
function, doone]; therefore, instead of the argument widths, these
first/last positions can be externally given to the function as new
argument(s) of your customized read.fwf.


Akio Sone

Harvard-MIT data center

> -----Original Message-----
> From: owner-r-help at stat.math.ethz.ch 
> [mailto:owner-r-help at stat.math.ethz.ch] On Behalf Of Brett A Magill
> Sent: Friday, May 03, 2002 12:54 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] skipping columns with read.fwf?
> I have a file in fwf.  It is rather large, about 40,000 rows 
> and 40 variables (columns).  I only need about 10 variables 
> form the data set for the analysis at hand.  Unfortunately, 
> these 10 variables are not contiguous in the file, for 
> example, the first is position 1-8, the next position 25-27, then 40.
> Is there a way to read the selected varaibles that I need 
> without reading in the entire data set?
> Thanks,
> Brett    
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> -.-.-.-.-.-.-.-.-
> r-help mailing list -- Read 
> http://www.ci.tuwien.ac.at/~hornik/R/R-> FAQ.html
> Send "info", 
> "help", or "[un]subscribe"
> (in the 
> "body", not the subject !)  To: 
> r-help-request at stat.math.ethz.ch 
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> _._._._._._._._._
> o

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list