简体   繁体   English

R 中的列中的相同名称

[英]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?在R中,我使用的是read_excel function,要导入一些文件,问题是我的文件有一些同名的列,有没有办法强制同名? (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() .您可以使用read_excel().name_repair参数来控制和关闭 tibble( 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:尽管确实知道重复的列名比“坏习惯”更接近语法错误,但生成的 object 将以奇怪的方式表现:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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