Rob Creel
Rob Creel

Reputation: 343

Make readxl::read_excel rename only the second duplicate column in R

In readr, the read_csv command handles duplicate column names by renaming the second duplicate and leaves the first unaltered. See the following example, taken from https://github.com/tidyverse/readxl/issues/53.

readr::read_csv("x,x,y\n1,2,3\n")
#> Warning: Duplicated column names deduplicated: 'x' => 'x_1' [2]
#> # A tibble: 1 × 3
#>       x   x_1     y
#>   <int> <int> <int>
#> 1     1     2     3

How can I get readxl::read_excel handle duplicate columns the same way?

Upvotes: 4

Views: 1140

Answers (1)

lroha
lroha

Reputation: 34441

You can use the .name_repair argument and pass make.unique() as a function:

library(readxl)

read_excel(path = "temp.xlsx", .name_repair = ~make.unique(.x, sep = "_"))

# A tibble: 1 x 3
      x   x_1     y
  <dbl> <dbl> <dbl>
1     1     2     3

Upvotes: 4

Related Questions