BaşlayınÜcretsiz başlayın

Parçaları bir data.frame olarak okumak

Önceki örnekte, her parçayı mstrsplit() kullanarak işleme fonksiyonuna bir matris olarak okumuştuk. Bu, her sütunda öğe türünün aynı olduğu dikdörtgen veri okurken uygundur. Böyle olmadığında, veriyi bir data.frame olarak okumak isteyebiliriz. Bunu, ya bir parçayı matris olarak okuyup sonra data.frame'e dönüştürerek ya da dstrsplit() fonksiyonunu kullanarak yapabilirsin.

Bu egzersiz, kursun bir parçasıdır

R'de Ölçeklenebilir Veri İşleme

Kursa Göz Atın

Egzersiz talimatları

  • make_msa_table() fonksiyonunda, her parçayı bir data frame olarak oku.
  • Veriyi parçalar halinde okumak için chunk.apply() çağır.
  • Her sütun için toplam sayıları, tüm satırları toplayarak elde et.

Uygulamalı etkileşimli egzersiz

Bu egzersizi bu örnek kodu tamamlayarak deneyin.

# Define the function to apply to each chunk
make_msa_table <- function(chunk) {
    # Read each chunk as a data frame
    x <- ___(chunk, col_types = rep("integer", length(col_names)), sep = ",")
    # Set the column names of the data frame that's been read
    colnames(x) <- col_names
    # Create new column, msa_pretty, with a string description of where the borrower lives
    x$msa_pretty <- msa_map[x$msa + 1]
    # Create a table from the msa_pretty column
    table(x$msa_pretty)
}

# Create a file connection to mortgage-sample.csv
fc <- file("mortgage-sample.csv", "rb")

# Read the first line to get rid of the header
readLines(fc, n = 1)

# Read the data in chunks
counts <- ___(fc, ___, CH.MAX.SIZE = 1e5)

# Close the file connection
close(fc)

# Aggregate the counts as before
___
Kodu Düzenle ve Çalıştır