借り手の人種・民族別(年別)(II)
この演習では、iotools と bigtabulate を使って、借り手の人種・民族を年ごとに集計します。
この演習はコースの一部です
Rで学ぶスケーラブルなデータ処理
演習の手順
iotools と bigtabulate はワークスペースに読み込まれています。
- チャンクを行列として読み込み、その後、借り手の人種と年で集計する関数
make_table()を作成します。 - こちらで用意したファイル接続からデータを取り込むために、
chunk.apply()を使います。 race_year_tableをデータフレームに変換します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Open a connection to the file and skip the header
fc <- file("mortgage-sample.csv", "rb")
readLines(fc, n = 1)
# Create a function to read chunks
make_table <- function(chunk) {
# Create a matrix
m <- ___(chunk, sep = ",", type = "integer")
colnames(m) <- mort_names
# Create the output table
___(m, c("borrower_race", "year"))
}
# Import data using chunk.apply
race_year_table <- ___(fc, make_table)
# Close connection
close(fc)
# Cast it to a data frame
rydf <- ___(race_year_table)
# Create a new column Race with race/ethnicity
rydf$Race <- race_cat