Borrower Race and Ethnicity by Year (II)
In this exercise, you'll use both iotools
and bigtabulate
to tabulate borrower race and ethnicity by year.
This exercise is part of the course
Scalable Data Processing in R
Exercise instructions
iotools
and bigtabulate
are loaded in your workspace.
- Create a function
make_table()
that reads in chunk as a matrix and then tabulates it by borrower race and year. - Use
chunk.apply()
to import the data from the file connection we created for you. - Convert
race_year_table
to a data frame.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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