read_table() and read_table2() are designed to read the type of textual data where each column is #' separate by one (or more) columns of space. read_table2() is like read.table(), it allows any number of whitespace characters between columns, and the lines can be of different lengths. read_table() is more strict, each line must be the same length, and each field is in the same position in every line. It first finds empty columns and then parses like a fixed width file. spec_table() and spec_table2() return the column specifications rather than a data frame.

read_table(file, col_names = TRUE, col_types = NULL,
  locale = default_locale(), na = "NA", skip = 0, n_max = Inf,
  guess_max = min(n_max, 1000), progress = show_progress(), comment = "")

read_table2(file, col_names = TRUE, col_types = NULL,
  locale = default_locale(), na = "NA", skip = 0, n_max = Inf,
  guess_max = min(n_max, 1000), progress = show_progress(), comment = "")

Arguments

file

Either a path to a file, a connection, or literal data (either a single string or a raw vector).

Files ending in .gz, .bz2, .xz, or .zip will be automatically uncompressed. Files starting with http://, https://, ftp://, or ftps:// will be automatically downloaded. Remote gz files can also be automatically downloaded and decompressed.

Literal data is most useful for examples and tests. It must contain at least one new line to be recognised as data (instead of a path).

col_names

Either TRUE, FALSE or a character vector of column names.

If TRUE, the first row of the input will be used as the column names, and will not be included in the data frame. If FALSE, column names will be generated automatically: X1, X2, X3 etc.

If col_names is a character vector, the values will be used as the names of the columns, and the first row of the input will be read into the first row of the output data frame.

Missing (NA) column names will generate a warning, and be filled in with dummy names X1, X2 etc. Duplicate column names will generate a warning and be made unique with a numeric prefix.

col_types

One of NULL, a cols() specification, or a string. See vignette("column-types") for more details.

If NULL, all column types will be imputed from the first 1000 rows on the input. This is convenient (and fast), but not robust. If the imputation fails, you'll need to supply the correct types yourself.

If a column specification created by cols(), it must contain one column specification for each column. If you only want to read a subset of the columns, use cols_only().

Alternatively, you can use a compact string representation where each character represents one column: c = character, i = integer, n = number, d = double, l = logical, D = date, T = date time, t = time, ? = guess, or _/- to skip the column.

locale

The locale controls defaults that vary from place to place. The default locale is US-centric (like R), but you can use locale() to create your own locale that controls things like the default time zone, encoding, decimal mark, big mark, and day/month names.

na

Character vector of strings to use for missing values. Set this option to character() to indicate no missing values.

skip

Number of lines to skip before reading data.

n_max

Maximum number of records to read.

guess_max

Maximum number of records to use for guessing column types.

progress

Display a progress bar? By default it will only display in an interactive session and not while knitting a document. The display is updated every 50,000 values and will only display if estimated reading time is 5 seconds or more. The automatic progress bar can be disabled by setting option readr.show_progress to FALSE.

comment

A string used to identify comments. Any text after the comment characters will be silently ignored.

See also

read_fwf() to read fixed width files where each column is not separated by whitespace. read_fwf() is also useful for reading tabular data with non-standard formatting.

Examples

# One corner from http://www.masseyratings.com/cf/compare.htm massey <- readr_example("massey-rating.txt") cat(read_file(massey))
#> UCC PAY LAZ KPK RT COF BIH DII ENG ACU Rank Team Conf #> 1 1 1 1 1 1 1 1 1 1 1 Ohio St B10 #> 2 2 2 2 2 2 2 2 4 2 2 Oregon P12 #> 3 4 3 4 3 4 3 4 2 3 3 Alabama SEC #> 4 3 4 3 4 3 5 3 3 4 4 TCU B12 #> 6 6 6 5 5 7 6 5 6 11 5 Michigan St B10 #> 7 7 7 6 7 6 11 8 7 8 6 Georgia SEC #> 5 5 5 7 6 8 4 6 5 5 7 Florida St ACC #> 8 8 9 9 10 5 7 7 10 7 8 Baylor B12 #> 9 11 8 13 11 11 12 9 14 9 9 Georgia Tech ACC #> 13 10 13 11 8 9 10 11 9 10 10 Mississippi SEC
read_table(massey)
#> Parsed with column specification: #> cols( #> UCC = col_integer(), #> PAY = col_integer(), #> LAZ = col_integer(), #> KPK = col_integer(), #> RT = col_integer(), #> COF = col_integer(), #> BIH = col_integer(), #> DII = col_integer(), #> ENG = col_integer(), #> ACU = col_integer(), #> Rank = col_integer(), #> Team = col_character(), #> Conf = col_character() #> )
#> # A tibble: 10 × 13 #> UCC PAY LAZ KPK RT COF BIH DII ENG ACU Rank #> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> #> 1 1 1 1 1 1 1 1 1 1 1 1 #> 2 2 2 2 2 2 2 2 2 4 2 2 #> 3 3 4 3 4 3 4 3 4 2 3 3 #> 4 4 3 4 3 4 3 5 3 3 4 4 #> 5 6 6 6 5 5 7 6 5 6 11 5 #> 6 7 7 7 6 7 6 11 8 7 8 6 #> 7 5 5 5 7 6 8 4 6 5 5 7 #> 8 8 8 9 9 10 5 7 7 10 7 8 #> 9 9 11 8 13 11 11 12 9 14 9 9 #> 10 13 10 13 11 8 9 10 11 9 10 10 #> # ... with 2 more variables: Team <chr>, Conf <chr>
# Sample of 1978 fuel economy data from # http://www.fueleconomy.gov/feg/epadata/78data.zip epa <- readr_example("epa78.txt") cat(read_file(epa))
#> ALFA ROMEO ALFA ROMEO 78010003 #> ALFETTA 03 81 8 74 7 89 9 ALFETTA 78010053 #> SPIDER 2000 01 SPIDER 2000 78010103 #> AMC AMC 78020002 #> GREMLIN 03 79 9 79 9 GREMLIN 78020053 #> PACER 04 89 11 89 11 PACER 78020103 #> PACER WAGON 07 90 26 91 26 PACER WAGON 78020153 #> CONCORD 04 88 12 90 11 90 11 83 16 CONCORD 78020203 #> CONCORD WAGON 07 91 30 91 30 CONCORD WAGON 78020253 #> MATADOR COUPE 05 97 14 97 14 MATADOR COUPE 78020303 #> MATADOR SEDAN 06 110 20 110 20 MATADOR SEDAN 78020353 #> MATADOR WAGON 09 112 50 112 50 MATADOR WAGON 78020403 #> ASTON MARTIN ASTON MARTIN 78040002 #> ASTON MARTIN ASTON MARTIN 78040053 #> AUDI AUDI 78050002 #> FOX 03 84 11 84 11 84 11 FOX 78050053 #> FOX WAGON 07 83 40 83 40 FOX WAGON 78050103 #> 5000 04 90 15 90 15 5000 78050153 #> AVANTI AVANTI 78065002 #> AVANTI II 02 75 8 75 8 AVANTI II 78065053
read_table(epa, col_names = FALSE)
#> Parsed with column specification: #> cols( #> X1 = col_character(), #> X2 = col_character(), #> X3 = col_integer(), #> X4 = col_integer(), #> X5 = col_integer(), #> X6 = col_integer(), #> X7 = col_integer(), #> X8 = col_integer(), #> X9 = col_integer(), #> X10 = col_integer(), #> X11 = col_character(), #> X12 = col_integer() #> )
#> # A tibble: 20 × 12 #> X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 #> <chr> <chr> <int> <int> <int> <int> <int> <int> <int> <int> #> 1 ALFA ROMEO NA NA NA NA NA NA NA NA #> 2 ALFETTA 03 81 8 74 7 89 9 NA NA #> 3 SPIDER 2000 01 NA NA NA NA NA NA NA NA #> 4 AMC NA NA NA NA NA NA NA NA #> 5 GREMLIN 03 79 9 NA NA NA NA 79 9 #> 6 PACER 04 89 11 NA NA NA NA 89 11 #> 7 PACER WAGON 07 90 26 91 26 NA NA NA NA #> 8 CONCORD 04 88 12 90 11 90 11 83 16 #> 9 CONCORD WAGON 07 91 30 NA NA 91 30 NA NA #> 10 MATADOR COUPE 05 97 14 97 14 NA NA NA NA #> 11 MATADOR SEDAN 06 110 20 NA NA 110 20 NA NA #> 12 MATADOR WAGON 09 112 50 NA NA 112 50 NA NA #> 13 ASTON MARTIN NA NA NA NA NA NA NA NA #> 14 ASTON MARTIN NA NA NA NA NA NA NA NA #> 15 AUDI NA NA NA NA NA NA NA NA #> 16 FOX 03 84 11 84 11 84 11 NA NA #> 17 FOX WAGON 07 83 40 NA NA 83 40 NA NA #> 18 5000 04 90 15 NA NA 90 15 NA NA #> 19 AVANTI NA NA NA NA NA NA NA NA #> 20 AVANTI II 02 75 8 75 8 NA NA NA NA #> # ... with 2 more variables: X11 <chr>, X12 <int>