EVOLUTION-MANAGER
Edit File: read.fwf.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Read Fixed Width Format Files</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for read.fwf {utils}"><tr><td>read.fwf {utils}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Read Fixed Width Format Files</h2> <h3>Description</h3> <p>Read a table of <b>f</b>ixed <b>w</b>idth <b>f</b>ormatted data into a <code><a href="../../base/html/data.frame.html">data.frame</a></code>. </p> <h3>Usage</h3> <pre> read.fwf(file, widths, header = FALSE, sep = "\t", skip = 0, row.names, col.names, n = -1, buffersize = 2000, fileEncoding = "", ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>file</code></td> <td> <p>the name of the file which the data are to be read from. </p> <p>Alternatively, <code>file</code> can be a <a href="../../base/html/connections.html">connection</a>, which will be opened if necessary, and if so closed at the end of the function call. </p> </td></tr> <tr valign="top"><td><code>widths</code></td> <td> <p>integer vector, giving the widths of the fixed-width fields (of one line), or list of integer vectors giving widths for multiline records.</p> </td></tr> <tr valign="top"><td><code>header</code></td> <td> <p>a logical value indicating whether the file contains the names of the variables as its first line. If present, the names must be delimited by <code>sep</code>.</p> </td></tr> <tr valign="top"><td><code>sep</code></td> <td> <p>character; the separator used internally; should be a character that does not occur in the file (except in the header).</p> </td></tr> <tr valign="top"><td><code>skip</code></td> <td> <p>number of initial lines to skip; see <code><a href="read.table.html">read.table</a></code>.</p> </td></tr> <tr valign="top"><td><code>row.names</code></td> <td> <p>see <code><a href="read.table.html">read.table</a></code>.</p> </td></tr> <tr valign="top"><td><code>col.names</code></td> <td> <p>see <code><a href="read.table.html">read.table</a></code>.</p> </td></tr> <tr valign="top"><td><code>n</code></td> <td> <p>the maximum number of records (lines) to be read, defaulting to no limit.</p> </td></tr> <tr valign="top"><td><code>buffersize</code></td> <td> <p>Maximum number of lines to read at one time</p> </td></tr> <tr valign="top"><td><code>fileEncoding</code></td> <td> <p>character string: if non-empty declares the encoding used on a file (not a connection) so the character data can be re-encoded. See the ‘Encoding’ section of the help for <code><a href="../../base/html/connections.html">file</a></code>, the ‘R Data Import/Export Manual’ and ‘Note’.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>further arguments to be passed to <code><a href="read.table.html">read.table</a></code>. Useful such arguments include <code>as.is</code>, <code>na.strings</code>, <code>colClasses</code> and <code>strip.white</code>.</p> </td></tr> </table> <h3>Details</h3> <p>Multiline records are concatenated to a single line before processing. Fields that are of zero-width or are wholly beyond the end of the line in <code>file</code> are replaced by <code>NA</code>. </p> <p>Negative-width fields are used to indicate columns to be skipped, e.g., <code>-5</code> to skip 5 columns. These fields are not seen by <code>read.table</code> and so should not be included in a <code>col.names</code> or <code>colClasses</code> argument (nor in the header line, if present). </p> <p>Reducing the <code>buffersize</code> argument may reduce memory use when reading large files with long lines. Increasing <code>buffersize</code> may result in faster processing when enough memory is available. </p> <p>Note that <code>read.fwf</code> (not <code>read.table</code>) reads the supplied file, so the latter's argument <code>encoding</code> will not be useful. </p> <h3>Value</h3> <p>A <code><a href="../../base/html/data.frame.html">data.frame</a></code> as produced by <code><a href="read.table.html">read.table</a></code> which is called internally. </p> <h3>Author(s)</h3> <p>Brian Ripley for <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> version: originally in <code>Perl</code> by Kurt Hornik. </p> <h3>See Also</h3> <p><code><a href="../../base/html/scan.html">scan</a></code> and <code><a href="read.table.html">read.table</a></code>. </p> <p><code><a href="read.fortran.html">read.fortran</a></code> for another style of fixed-format files. </p> <h3>Examples</h3> <pre> ff <- tempfile() cat(file = ff, "123456", "987654", sep = "\n") read.fwf(ff, widths = c(1,2,3)) #> 1 23 456 \ 9 87 654 read.fwf(ff, widths = c(1,-2,3)) #> 1 456 \ 9 654 unlink(ff) cat(file = ff, "123", "987654", sep = "\n") read.fwf(ff, widths = c(1,0, 2,3)) #> 1 NA 23 NA \ 9 NA 87 654 unlink(ff) cat(file = ff, "123456", "987654", sep = "\n") read.fwf(ff, widths = list(c(1,0, 2,3), c(2,2,2))) #> 1 NA 23 456 98 76 54 unlink(ff) </pre> <hr /><div style="text-align: center;">[Package <em>utils</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>