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.
Cet exercice fait partie du cours
Scalable Data Processing in R
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.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de 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