開始使用免費開始

將分塊讀入為矩陣

在這個練習中,你要撰寫一個可擴充的 table() 函式,利用 chunk.apply() 計算房貸資料集裡的都會(urban)與鄉村(rural)借款人數。chunk.apply() 預設會用 rbind() 聚合已處理的資料。也就是說,你可以先對每個分塊建立一個表格,再把結果矩陣的各列相加,就能得到整體的計數。

我們已為 "mortgage-sample.csv" 檔案建立連線 fc,並讀取第一行以移除標頭。

本練習屬於課程

R 的可擴展資料處理

檢視課程

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Define the function to apply to each chunk
make_table <- function(chunk) {
    # Read each chunk as a matrix
    x <- ___(chunk, type = "integer", sep = ",")
    # Create a table of the number of borrowers (column 3) for each chunk
    table(x[, 3])
}
編輯並執行程式碼