女性借款比例
在上一個練習中,你以年份與種族(或族裔)進行分層。不過,還有許多其他分割資料的方式。接下來這兩題,你會依年份計算都會區與鄉村地區的女性借款人比例。這題與上一題略有不同:你不是只要計算筆數,而是要取得在特定年份條件下的女性借款人「比例」。
在這題中,我們已經定義了一個函式 female_residence_prop(),用來計算都會區與鄉村地區的女性借款人比例。
本練習屬於課程
R 的可擴展資料處理
練習說明
• 呼叫 female_residence_prop(),找出 2015 年都會區與鄉村地區女性借款人的比例:
• 第一個引數為資料 mort。
• 第二個引數為對應 2015 年列編號的邏輯向量。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
female_residence_prop <- function(x, rows) {
x_subset <- x[rows, ]
# Find the proportion of female borrowers in urban areas
prop_female_urban <- sum(x_subset[, "borrower_gender"] == 2 &
x_subset[, "msa"] == 1) /
sum(x_subset[, "msa"] == 1)
# Find the proportion of female borrowers in rural areas
prop_female_rural <- sum(x_subset[, "borrower_gender"] == 2 &
x_subset[, "msa"] == 0) /
sum(x_subset[, "msa"] == 0)
c(prop_female_urban, prop_female_rural)
}
# Find the proportion of female borrowers in 2015
___(___, ___)