简体   繁体   中英

Same names in Columns in R

In R, I am using a read_excel function, to import some files, the problem is that my files have some columns with the same name, is there any way to force the same name? (I know it's not a good practice, but it's a very specific thing)

New names:
* `44228` -> `44228...4`
* `44229` -> `44229...5`
* `44230` -> `44230...6`
* `44231` -> `44231...7`
* `44232` -> `44232...8`

I need to use a conversion factor for these data names, so I need to leave it with the name of the member, they are data.

You can use the .name_repair argument of read_excel() to control, and turn off, the checks applied to column names by tibble() . So to allow duplicate names:

library("readxl")
library("writexl") # Only needed to generate an example xlsx file

x <- data.frame(a = 1:3, a = 1:3, a = 1:3, check.names = FALSE)
write_xlsx(x, "data.xlsx")

read_xlsx("data.xlsx", .name_repair = "minimal")
#> # A tibble: 3 x 3
#>       a     a     a
#>   <dbl> <dbl> <dbl>
#> 1     1     1     1
#> 2     2     2     2
#> 3     3     3     3

Although do be aware that duplicate column names are closer to a syntax error than "bad practice", so the resulting object will behave in strange ways:

df <- read_xlsx("data.xlsx", .name_repair = "minimal")
df$a
#> [1] 1 2 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM