IniziaInizia gratis

Razza ed etnia dei mutuatari per anno (II)

In questo esercizio userai sia iotools sia bigtabulate per tabulare razza ed etnia dei mutuatari per anno.

Questo esercizio fa parte del corso

Elaborazione scalabile dei dati in R

Visualizza il corso

Istruzioni dell'esercizio

iotools e bigtabulate sono già caricati nel tuo workspace.

  • Crea una funzione make_table() che legga un blocco come matrice e poi lo tabuli per razza del mutuatario e anno.
  • Usa chunk.apply() per importare i dati dalla connessione al file che abbiamo creato per te.
  • Converti race_year_table in un data frame.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

# 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
Modifica ed esegui il codice