女性借款者占比
在上一个练习中,您按年份和种族(或族裔)进行了分层。不过,划分数据还有很多其他方式。本练习和下一个练习中,您将按年份计算城市与农村地区中女性借款者的占比。这个练习与上一个略有不同,因为您不再只是计算数量,而是要在给定年份的前提下,得到女性借款者的「比例」。
在本练习中,我们已经定义了一个函数,用于计算城市与农村地区中女性借款者的比例: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
___(___, ___)