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()
读取速度会快得多。
1.5.1 安装
# 从cran安装
install.packages("vroom")
# install.packages("devtools")
::install_dev("vroom") devtools
1.5.2 用法
- 读取文件
library(vroom)
<- vroom_example("mtcars.csv")
file
file
vroom(file)
vroom(file, delim = ",")
- 读取多文件
即vroom::vroom()
具备迭代效果,具体情况如下:
<- tibble::rownames_to_column(mtcars, "model")
mt ::iwalk(
purrrsplit(mt, mt$cyl),
~ vroom_write(.x, glue::glue("mtcars_{.y}.csv"), "\t")
)
<- fs::dir_ls(glob = "mtcars*csv")
files
files
# read_csv
::map_dfr(files,readr::read_delim,delim="\t")
purrr
# vroom same above
vroom(files)
- 读取压缩文件
vroom支持zip,gz,bz2,xz等压缩文件,只需要将压缩文件名称传递给vroom即可。
<- vroom_example("mtcars.csv.gz")
file
vroom(file)
- 读取网络文件
<- "https://raw.githubusercontent.com/r-lib/vroom/master/inst/extdata/mtcars.csv"
file vroom(file)
- 选择列读取
room提供了与dplyr::select()
相同的列选择和重命名接口
<- vroom_example("mtcars.csv.gz")
file
vroom(file, col_select = c(model, cyl, gear))
# vroom(file, col_select = c(1, 3, 11))
# vroom(file, col_select = list(car = model, everything()))