按年份统计借款人的种族与族裔(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