1.5 vroom

vroom实现读取矩形数据到R中,如 comma separated(csv),tab separated(tsv), fixed width files(fwf)。该包的功能类似readr::read_csv(),data.table::fread()read.csv(),但是对于许多数据集来说,vroom::vroom()读取速度会快得多。

vroom项目地址

1.5.1 安装

# 从cran安装
install.packages("vroom")
# install.packages("devtools")
devtools::install_dev("vroom")

1.5.2 用法

  1. 读取文件
library(vroom)
file <- vroom_example("mtcars.csv")
file

vroom(file)
vroom(file, delim = ",")
  1. 读取多文件

vroom::vroom()具备迭代效果,具体情况如下:

mt <- tibble::rownames_to_column(mtcars, "model")
purrr::iwalk(
  split(mt, mt$cyl),
  ~ vroom_write(.x, glue::glue("mtcars_{.y}.csv"), "\t")
)

files <- fs::dir_ls(glob = "mtcars*csv")
files

# read_csv

purrr::map_dfr(files,readr::read_delim,delim="\t")

# vroom same above
vroom(files) 
  1. 读取压缩文件

vroom支持zip,gz,bz2,xz等压缩文件,只需要将压缩文件名称传递给vroom即可。

file <- vroom_example("mtcars.csv.gz")

vroom(file)
  1. 读取网络文件
file <- "https://raw.githubusercontent.com/r-lib/vroom/master/inst/extdata/mtcars.csv"
vroom(file)
  1. 选择列读取

room提供了与dplyr::select()相同的列选择和重命名接口

file <- vroom_example("mtcars.csv.gz")

vroom(file, col_select = c(model, cyl, gear))

# vroom(file, col_select = c(1, 3, 11))

# vroom(file, col_select = list(car = model, everything()))