R for data science计算机@linux_python_R 技术帖

rio极简数据导入教程

2020-04-17  本文已影响0人  柳叶刀与小鼠标

rio 包的目的是使得数据导入尽可能的简单化。

示例

这里有一些示例包括:读入,写出,转换。

写出

主要由export()函数完成

library("rio")
export(mtcars, "mtcars.csv") # comma-separated values
export(mtcars, "mtcars.rds") # R serialized
export(mtcars, "mtcars.sav") # SPSS

并且rio包的export函数支持将数据保存为压缩格式的文件

export(mtcars, "mtcars.tsv.zip")

export() 函数同样可以保存多个数据库为一个文件,例如Excel 文件

export(list(mtcars = mtcars, iris = iris), file = "mtcars.xlsx")

写入

主要由import()来完成

x <- import("mtcars.csv")
y <- import("mtcars.rds")
z <- import("mtcars.sav")
# confirm data match
all.equal(x, y, check.attributes = FALSE)
## [1] TRUE
all.equal(x, z, check.attributes = FALSE)
## [1] TRUE

现在rio包支持读取multi object的文件例如(Excel workbook, .Rdata file, zip directory, or HTML file)

str(m <- import_list("mtcars.xlsx"))
## List of 2
##  $ mtcars:'data.frame':  32 obs. of  11 variables:
##   ..$ mpg : num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##   ..$ cyl : num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
##   ..$ disp: num [1:32] 160 160 108 258 360 ...
##   ..$ hp  : num [1:32] 110 110 93 110 175 105 245 62 95 123 ...
##   ..$ drat: num [1:32] 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##   ..$ wt  : num [1:32] 2.62 2.88 2.32 3.21 3.44 ...
##   ..$ qsec: num [1:32] 16.5 17 18.6 19.4 17 ...
##   ..$ vs  : num [1:32] 0 0 1 1 0 1 0 1 1 1 ...
##   ..$ am  : num [1:32] 1 1 1 0 0 0 0 0 0 0 ...
##   ..$ gear: num [1:32] 4 4 4 3 3 3 3 4 4 4 ...
##   ..$ carb: num [1:32] 4 4 1 1 2 1 4 2 2 4 ...
##  $ iris  :'data.frame':  150 obs. of  5 variables:
##   ..$ Sepal.Length: num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##   ..$ Sepal.Width : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##   ..$ Petal.Length: num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##   ..$ Petal.Width : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##   ..$ Species     : chr [1:150] "setosa" "setosa" "setosa" "setosa" ...

将multi object文件保存

export_list(m, "%s.tsv")
## Error in export_list(m, "%s.tsv"): could not find function "export_list"
c("mtcars.tsv", "iris.tsv") %in% dir()
## [1] FALSE FALSE

转换

convert() 函数将 import() 函数和export() 函数合二为一,可以实时的读取文件同时将它保存。

convert("mtcars.sav", "mtcars.dta")

同样可以在命令行中直接读取 Stata (.dta) ,并将它转化保存为(.csv),

Rscript -e "rio::convert('iris.dta', 'iris.csv')"

支持的文件类型,如下所示

install_formats()
Format Typical Extension Import Package Export Package Installed by Default
Comma-separated data .csv data.table data.table Yes
Pipe-separated data .psv data.table data.table Yes
Tab-separated data .tsv data.table data.table Yes
CSVY (CSV + YAML metadata header) .csvy data.table data.table Yes
SAS .sas7bdat haven haven Yes
SPSS .sav haven haven Yes
Stata .dta haven haven Yes
SAS XPORT .xpt haven haven Yes
SPSS Portable .por haven Yes
Excel .xls readxl Yes
Excel .xlsx readxl openxlsx Yes
R syntax .R base base Yes
Saved R objects .RData, .rda base base Yes
Serialized R objects .rds base base Yes
Epiinfo .rec foreign Yes
Minitab .mtp foreign Yes
Systat .syd foreign Yes
"XBASE" database files .dbf foreign foreign Yes
Weka Attribute-Relation File Format .arff foreign foreign Yes
Data Interchange Format .dif utils Yes
Fortran data no recognized extension utils Yes
Fixed-width format data .fwf utils utils Yes
gzip comma-separated data .csv.gz utils utils Yes
Apache Arrow (Parquet) .parquet arrow arrow No
EViews .wf1 hexView No
Feather R/Python interchange format .feather feather feather No
Fast Storage .fst fst fst No
JSON .json jsonlite jsonlite No
Matlab .mat rmatio rmatio No
OpenDocument Spreadsheet .ods readODS readODS No
HTML Tables .html xml2 xml2 No
Shallow XML documents .xml xml2 xml2 No
YAML .yml yaml yaml No
Clipboard default is tsv clipr clipr No
Google Sheets as Comma-separated data
Graphpad Prism .pzfx pzfx pzfx No

如果是rio包无法识别的文件类型,则会返回下列的报错:"Unrecognized file format"

安装rio包

经典版本

install.packages("rio")
install_formats()

最近版本:

if (!require("remotes")){
    install.packages("remotes")
}
remotes::install_github("leeper/rio")
上一篇下一篇

猜你喜欢

热点阅读