เชื้อชาติและกลุ่มชาติพันธุ์ของผู้กู้จำแนกตามปี (II)
ในแบบฝึกหัดนี้ จะใช้ทั้ง iotools และ bigtabulate เพื่อจัดทำตารางข้อมูลเชื้อชาติและกลุ่มชาติพันธุ์ของผู้กู้จำแนกตามปี
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การประมวลผลข้อมูลขนาดใหญ่ใน R
คำแนะนำการฝึกหัด
โหลด iotools และ bigtabulate ไว้ใน workspace ของคุณแล้ว
- สร้างฟังก์ชัน
make_table()ที่อ่าน chunk เป็น matrix แล้วจัดทำตารางข้อมูลตามเชื้อชาติของผู้กู้และปี - ใช้
chunk.apply()เพื่อนำเข้าข้อมูลจาก file connection ที่เตรียมไว้ให้ - แปลง
race_year_tableเป็น data frame
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# 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