借款者的種族與族裔(依年份)(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