library(readr)
library(microbenchmark) #速度比較に利用
library(data.table) #速度比較に利用readr
パッケージの概要
readrは、高速かつ直感的なデータ読み込み機能を提供するパッケージです。また、このパッケージは、Rでモダンな分析環境を構築するためのパッケージ群である「tidyverse」に含まれています。
データの読込にはR標準の関数が存在しています。例えば、read.csv関数を利用すると、カンマ区切り(,)のcsvファイルを読み込むことができます。
data <- read.csv("../data/insurance.csv")
head(data) age sex bmi children smoker region expenses
1 19 female 27.9 0 yes southwest 16884.92
2 18 male 33.8 1 no southeast 1725.55
3 28 male 33.0 3 no southeast 4449.46
4 33 male 22.7 0 no northwest 21984.47
5 32 male 28.9 0 no northwest 3866.86
6 31 female 25.7 0 no southeast 3756.62
read_csv関数
readrでカンマ区切り(,)のファイルを読み込む際は、read_csv関数を利用します。 なお、タブ区切りのファイルを読み込む場合は、read_tsv関数を利用します。また、read_delim関数は区切り文字を任意で指定することができます。
read_csv関数とread.csv関数の違い
read_csv関数とread.csv関数の違いは、例えば、読み込んだデータの項目の型が何であるかを明示的に説明してくれることが挙げられます。 また、データはdata.frameではなく、tibbleで読み込まれます。
data_readr <- read_csv("../data/insurance.csv")Rows: 1338 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (3): sex, smoker, region
dbl (4): age, bmi, children, expenses
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
data_readr# A tibble: 1,338 × 7
age sex bmi children smoker region expenses
<dbl> <chr> <dbl> <dbl> <chr> <chr> <dbl>
1 19 female 27.9 0 yes southwest 16885.
2 18 male 33.8 1 no southeast 1726.
3 28 male 33 3 no southeast 4449.
4 33 male 22.7 0 no northwest 21984.
5 32 male 28.9 0 no northwest 3867.
6 31 female 25.7 0 no southeast 3757.
7 46 female 33.4 1 no southeast 8241.
8 37 female 27.7 3 no northwest 7282.
9 37 male 29.8 2 no northeast 6406.
10 60 female 25.8 0 no northwest 28923.
# ℹ 1,328 more rows
class(data)[1] "data.frame"
tibbleは、従来のdata.frameを洗練させたデータ構造です。readrと同様、「tidyverse」に含まれるtibbleというパッケージで作成することができます。
型の指定
read_csv関数で読み込んだデータの各項目の型は自動で推定されますが、明示的に指定することもできます。型を指定する際は、col_types引数に、項目名と型を指定する関数を記載します。
以下のコードではageの型をintegerに指定して、データを読み込みます。なお、指定しなかった項目の型は自動で推定されます。
data_readr_2 <- read_csv("../data/insurance.csv",
col_types = cols( age = col_integer()))
data_readr_2# A tibble: 1,338 × 7
age sex bmi children smoker region expenses
<int> <chr> <dbl> <dbl> <chr> <chr> <dbl>
1 19 female 27.9 0 yes southwest 16885.
2 18 male 33.8 1 no southeast 1726.
3 28 male 33 3 no southeast 4449.
4 33 male 22.7 0 no northwest 21984.
5 32 male 28.9 0 no northwest 3867.
6 31 female 25.7 0 no southeast 3757.
7 46 female 33.4 1 no southeast 8241.
8 37 female 27.7 3 no northwest 7282.
9 37 male 29.8 2 no northeast 6406.
10 60 female 25.8 0 no northwest 28923.
# ℹ 1,328 more rows
速度比較
read_csvがデータを読み込む速度は、read.csvよりも速いです。どの程度速くなるかはデータの構造、サイズによっても異なります。40MB程度の容量のcsvファイルを利用して、実際に速度比較を実施してみます。
data_readr_3 <- read_csv("../data/lapse_study.csv")Rows: 345627 Columns: 16
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (9): duration, issue_year, gender, issue_age, face_amount, post_level_pr...
dbl (7): id, study_year, exposure_amount, exposure_count, lapse_amount, laps...
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(data_readr_3)# A tibble: 6 × 16
id study_year duration issue_year gender issue_age face_amount
<dbl> <dbl> <chr> <chr> <chr> <chr> <chr>
1 1 2001 06-09 1991-1993 female 0-19 -100K
2 2 2001 06-09 1991-1993 female 0-19 -100K
3 3 2001 06-09 1991-1993 female 0-19 -100K
4 4 2001 06-09 1991-1993 female 0-19 -100K
5 5 2001 06-09 1991-1993 female 0-19 -100K
6 6 2001 06-09 1991-1993 female 0-19 -100K
# ℹ 9 more variables: post_level_premium_structure <chr>,
# premium_jump_ratio <chr>, risk_class <chr>, premium_mode <chr>,
# exposure_amount <dbl>, exposure_count <dbl>, lapse_amount <dbl>,
# lapse_count <dbl>, lapse_rate <dbl>
read.csvに加えて、data.tableパッケージのfreadとも速度比較をします。microbenchmarkパッケージを利用して、各関数で5回ずつデータの読込を実施します。
microbenchmark(
base_read = read.csv("../data/lapse_study.csv"),
readr_read = read_csv("../data/lapse_study.csv",
show_col_types = FALSE),
dt_fread = fread("../data/lapse_study.csv"),
times = 5
)Unit: milliseconds
expr min lq mean median uq max neval
base_read 1367.5526 1431.1314 1528.0558 1444.7041 1636.7175 1760.1736 5
readr_read 311.0049 313.5210 350.3571 317.6245 357.8283 451.8066 5
dt_fread 120.6556 120.7854 161.1737 135.0461 208.7452 220.6364 5
cld
a
b
c
read_csvの平均的な読込時間は、read.csvよりも速く、freadよりも遅くなりました。
freadはデータの読込速度について、read_csvよりも速くなります。一方、tidyverseとの親和性は、読込結果がtibbleで出力されるread_csv(freadの出力はdata.table)のほうが高くなります。