vroom package 提供了一种在导入期间按名称选择/删除列的“整洁”方法。文档:https://www.tidyverse.org/blog/2019/05/vroom-1-0-0/#column-selection
列选择(col_select)
vroom 参数“col_select”使选择要保留(或省略)的列更加直接。 col_select 的接口与 dplyr::select() 相同。
按名称选择列
data <- vroom("flights.tsv", col_select = c(year, flight, tailnum))
#> Observations: 336,776
#> Variables: 3
#> chr [1]: tailnum
#> dbl [2]: year, flight
#>
#> Call `spec()` for a copy-pastable column specification
#> Specify the column types with `col_types` to quiet this message
按名称删除列
data <- vroom("flights.tsv", col_select = c(-dep_time, -air_time:-time_hour))
#> Observations: 336,776
#> Variables: 13
#> chr [4]: carrier, tailnum, origin, dest
#> dbl [9]: year, month, day, sched_dep_time, dep_delay, arr_time, sched_arr_time, arr...
#>
#> Call `spec()` for a copy-pastable column specification
#> Specify the column types with `col_types` to quiet this message
Use the selection helpers
data <- vroom("flights.tsv", col_select = ends_with("time"))
#> Observations: 336,776
#> Variables: 5
#> dbl [5]: dep_time, sched_dep_time, arr_time, sched_arr_time, air_time
#>
#> Call `spec()` for a copy-pastable column specification
#> Specify the column types with `col_types` to quiet this message
加载多个文件并选择特定列并跳过第一行: H4>
files <- fs::dir_ls(glob = "SALES*2019.csv")
data <- vroom(files, col_select = c(article_1, article_2, article_3, etc), skip = 1)