使 readxl::read_excel 仅重命名 R 中的第二个重复列

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

在 readr 中,read_csv 命令通过重命名第二个重复项来处理重复的列名,并保持第一个不变。请参阅以下示例,摘自 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

我怎样才能readxl::read_excel以相同的方式处理重复的列?

您可以使用 .name_repair 参数并将 make.unique() 作为函数传递:

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