เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

Borrower Region by Year

In this exercise you'll tabulate the data by year and the msa (city vs rural) variable.

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Scalable Data Processing in R

ดูคอร์ส

คำแนะนำการฝึกหัด

All the required packages are loaded in your workspace.

  • Create a function make_table() that reads in chunk as a matrix and then tabulates it by borrower region (msa) and year.
  • Use chunk.apply() to import the data from the file connection we created for you.
  • Run the rest of the code to plot the changes in mortgages received by region.

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# 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 <- ___(___, sep = ",", type = "integer")
    colnames(m) <- mort_names
    # Create the output table
    ___(___, c(___, ___))
}

# Import data using chunk.apply
msa_year_table <- ___

# Close connection
close(fc)

# Convert to a data frame
df_msa <- as.data.frame(msa_year_table)

# Rename columns
df_msa$MSA <- c("rural", "city")

# Gather on all columns except Year
df_msa_long <- pivot_longer(df_msa, -MSA, names_to = "Year", values_to = "Count")

# Plot 
ggplot(df_msa_long, aes(x = Year, y = Count, group = MSA, color = MSA)) + 
    geom_line()
แก้ไขและรันโค้ด