Read Fixed-Format Data in a Fortran-like Style

Read fixed-format data files using Fortran-style format specifications.

file, connection
read.fortran(file, format, ..., = TRUE, colClasses = NA)

File or connection to read from.


Character vector or list of vectors. See ‘Details’ below.

Other arguments for read.fwf.

Keep characters as characters?


Variable classes to override defaults. See read.table for details.


The format for a field is of one of the following forms: rFl.d, rDl.d, rXl, rAl, rIl, where l is the number of columns, d is the number of decimal places, and r is the number of repeats. F and D are numeric formats, A is character, I is integer, and X indicates columns to be skipped. The repeat code r and decimal place code d are always optional. The length code l is 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.

Skipped (X) columns are not passed to read.fwf, so colClasses, 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 F or D format will shift the decimal d places to the left, even if it is explicitly specified in the input file.

See Also

read.fwf, read.table, read.csv

  • read.fortran
library(utils) # NOT RUN { 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) # }
Documentation reproduced from package utils, version 3.6.2, License: Part of R 3.6.2

Community examples

tomaz-suller at Dec 26, 2020 utils v3.6.2

First, analyse the data you're working with and check the **number of columns** (width) each entry occupies `readLines("", n=5)` It's important to note there is a whitespace before each entry, and that columns are separated by 5 whitespaces. Each `Week` has a width of 9 characters, and each of the 8 numeric entries has width 4 (including decimal point and minus sign for SSTA). Therefore, we want out format to be: `format = c("X1", "A9", "X5", "2F4", "X5", "2F4", "X5", "2F4", "X5", "2F4")` This means: ignore the first whitespace (`X1`) and the ones between the columns (`X5`); read the 9 characters (`A9`); and the two numeric entries. Since they're concatenated, we can repeat the format twice, so we get `2F4`. Now we can read the data into a `data.frame`: `sst = read.fortran("", format, skip=4)` Notice it's necessary to skip the header in order to read the data correctly. Otherwise, `read.fortran` will try to match out format from the first line of the file, giving an error. Finally, you may want to give the columns of the `data.frame` a name using `colnames`