【问题标题】:Remove automatically all spaces from column names using read_excel使用 read_excel 自动删除列名中的所有空格
【发布时间】:2020-12-04 04:00:51
【问题描述】:

我的 .xlsx Excel 工作表中的列名有空格。如何自动将空格替换为“_”或“.”?我想使用read_excel,因为我需要在我的 Excel 工作表中指定一个范围。

【问题讨论】:

  • This post 可能会有所帮助。
  • read_excel() 中使用.name_repair 参数。例如,.name_repair = "universal",或者创建一个函数来做你想做的事。有关.name_repair 参数的更多信息,请参阅?tibble::tibble

标签: r readxl


【解决方案1】:

这是一种使用.name_repair 参数和read_excel() 的方法:

创建用于导入的 excel 文件

# Example setup
mtcars = datasets::mtcars
names(mtcars) = paste(names(mtcars), LETTERS[1:length(mtcars)])
head(mtcars)
                  mpg A cyl B disp C hp D drat E  wt F qsec G vs H am I gear J carb K
Mazda RX4          21.0     6    160  110   3.90 2.620  16.46    0    1      4      4
Mazda RX4 Wag      21.0     6    160  110   3.90 2.875  17.02    0    1      4      4
Datsun 710         22.8     4    108   93   3.85 2.320  18.61    1    1      4      1
Hornet 4 Drive     21.4     6    258  110   3.08 3.215  19.44    1    0      3      1
Hornet Sportabout  18.7     8    360  175   3.15 3.440  17.02    0    0      3      2
Valiant            18.1     6    225  105   2.76 3.460  20.22    1    0      3      1

temp = tempfile(fileext = ".xlsx")
writexl::write_xlsx(mtcars, temp)

以几种不同的方式读取数据

# With using the default for .name_repair ("unique" for read_excel())
head(readxl::read_excel(temp))
# A tibble: 6 x 11
  `mpg A` `cyl B` `disp C` `hp D` `drat E` `wt F` `qsec G` `vs H` `am I` `gear J` `carb K`
    <dbl>   <dbl>    <dbl>  <dbl>    <dbl>  <dbl>    <dbl>  <dbl>  <dbl>    <dbl>    <dbl>
1    21         6      160    110     3.9    2.62     16.5      0      1        4        4
2    21         6      160    110     3.9    2.88     17.0      0      1        4        4
3    22.8       4      108     93     3.85   2.32     18.6      1      1        4        1
4    21.4       6      258    110     3.08   3.22     19.4      1      0        3        1
5    18.7       8      360    175     3.15   3.44     17.0      0      0        3        2
6    18.1       6      225    105     2.76   3.46     20.2      1      0        3        1


# Adding periods using using .name_repair
head(readxl::read_excel(temp, .name_repair = "universal"))
# A tibble: 6 x 11
  mpg.A cyl.B disp.C  hp.D drat.E  wt.F qsec.G  vs.H  am.I gear.J carb.K
  <dbl> <dbl>  <dbl> <dbl>  <dbl> <dbl>  <dbl> <dbl> <dbl>  <dbl>  <dbl>
1  21       6    160   110   3.9   2.62   16.5     0     1      4      4
2  21       6    160   110   3.9   2.88   17.0     0     1      4      4
3  22.8     4    108    93   3.85  2.32   18.6     1     1      4      1
4  21.4     6    258   110   3.08  3.22   19.4     1     0      3      1
5  18.7     8    360   175   3.15  3.44   17.0     0     0      3      2
6  18.1     6    225   105   2.76  3.46   20.2     1     0      3      1

# Using a custom function to add underscores
head(readxl::read_excel(temp, .name_repair = function(x) gsub("\\s+", "_", x)))
# A tibble: 6 x 11
  mpg_A cyl_B disp_C  hp_D drat_E  wt_F qsec_G  vs_H  am_I gear_J carb_K
  <dbl> <dbl>  <dbl> <dbl>  <dbl> <dbl>  <dbl> <dbl> <dbl>  <dbl>  <dbl>
1  21       6    160   110   3.9   2.62   16.5     0     1      4      4
2  21       6    160   110   3.9   2.88   17.0     0     1      4      4
3  22.8     4    108    93   3.85  2.32   18.6     1     1      4      1
4  21.4     6    258   110   3.08  3.22   19.4     1     0      3      1
5  18.7     8    360   175   3.15  3.44   17.0     0     0      3      2
6  18.1     6    225   105   2.76  3.46   20.2     1     0      3      1

# file.remove(temp)

【讨论】:

    【解决方案2】:

    没有办法在方法read_excel() 中告诉它这样做,但是你可以做两件事来规避这个问题。

    #create list of column names as you wish them to appear manually
    names_vector<- c("name-1", "name_2"....)
    
    #set the col_names argument equal to the list of names
    data<-read_excel( <file_name>, col_names =names_vector)
    

    这两个步骤将用正确的名称替换 XLSX 文件中的标题。

    【讨论】:

      【解决方案3】:

      使用这个 .xlsx 文件:

      我使用了库openxlsx。使用read.xlsx 函数我得到了以下数据框:

           a.long.col.name yet.another.second.column
      1               a                         1
      2               b                         2
      3               c                         3
      4               d                         4
      

      read.xlsx 还包含从 Excel 表中选择行和列的选项:

      read.xlsx('C:/Users/<username>/Documents/so_query.xlsx',
            rows  = 2:5,
            cols = 2)
      

      提供以下数据框:

        1
      1 2
      2 3
      3 4
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-10
        • 2019-08-13
        • 2013-06-22
        • 1970-01-01
        • 2011-11-03
        相关资源
        最近更新 更多