Read Fixed-Format Data in a Fortran-like Style
Read fixed-format data files using Fortran-style format specifications.
File or connection to read from.
Character vector or list of vectors. See ‘Details’ below.
Other arguments for
Keep characters as characters?
Variable classes to override defaults. See
The format for a field is of one of the following forms:
the number of columns,
d is the number of decimal places, and
r is the number of repeats.
D are numeric
A is character,
I is integer, and
indicates columns to be skipped. The repeat code
r and decimal
d are always optional. The length code
required except for
X formats when
r is present.
For a single-line record,
format should be a character
vector. For a multiline record it should be a list with a character
vector for each line.
X) columns are not passed to
col.names, and similar arguments passed to
read.fwf should not reference these columns.
A data frame
read.fortran does not use actual Fortran input routines, so
the formats are at best rough approximations to the Fortran ones.
In particular, specifying
d > 0 in the
format will shift the decimal
d places to the left, even if
it is explicitly specified in the input file.
1 2 3 4 5 6 7 8 9 10 11 12 13
ff <- tempfile() cat(file = ff, "123456", "987654", sep = "\n") read.fortran(ff, c("F2.1","F2.0","I2")) read.fortran(ff, c("2F1.0","2X","2A1")) unlink(ff) cat(file = ff, "123456AB", "987654CD", sep = "\n") read.fortran(ff, list(c("2F3.1","A2"), c("3I2","2X"))) unlink(ff) # Note that the first number is read differently than Fortran would # read it: cat(file = ff, "12.3456", "1234567", sep = "\n") read.fortran(ff, "F7.4") unlink(ff)