Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

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.

question from:https://stackoverflow.com/questions/66063663/same-names-in-columns-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...