library(readxl)

Column names via col_names

readxl has always let you specify col_names explicitly at the time of import:

But users have long wanted a way to specify a name repair strategy, as opposed to enumerating the actual column names.

Built-in levels of .name_repair

As of v1.2.0, readxl provides the .name_repair argument, which affords control over how column names are checked or repaired. This requires v2.0.0 or higher of the tibble package, which powers this feature under the hood. December 2018 note: tibble v2.0.0 hasn’t been released yet but will be soon. Until then, .name_repair requires a development version of tibble.

The .name_repair argument in read_excel(), read_xls(), and read_xlsx() works exactly the same way as it does in tibble::tibble() and tibble::as_tibble(). Full documentation is in the ?name-repair topic of tibble.

readxl’s default is .name_repair = "unique", which ensures each column has a unique name. If that is already true of the column names, readxl won’t touch them.

The value .name_repair = "universal" goes further and makes column names syntactic, i.e. makes sure they don’t contain any forbidden characters or reserved words. This makes life easier if you use packages like ggplot2 and dplyr downstream, because the column names will “just work” everywhere and won’t require protection via backtick quotes.

Compare the column names in these two calls. This shows the difference between "unique" (names can contain spaces) and "universal" (spaces replaced by .).

If you don’t want readxl to touch your column names at all, use .name_repair = "minimal".

Pass a function to .name_repair

The .name_repair argument also accepts a function – pre-existing or written by you – or an anonymous formula. This function must operate on a “names in, names out” basis.

This means you can also perform name repair in the style of base R or another package, such as janitor::make_clean_names() (requires janitor > v1.1.1).

What if you have a spreadsheet with lots of missing column names? Here’s how you could fall back to letter-based column names, for easier troubleshooting.