在R.对于低收入和高收入,每个人获得贷款的百分比(Loan_Given =='Y')。下面是我的数据集。
Income_Level <- c('High','Low','Low','High',
'Low','High','High','High','Low','Low','High','Low')
Loan_Given <- c('Y','N','N','Y','Y','Y','N','N','Y','N','Y','Y')
data.frame(Income_Level,Loan_Given)
我感谢任何帮助。感谢
答案 0 :(得分:0)
使用tidyverse
工具,这是group_by
,然后是summarise
:
Income_Level <- c('High','Low','Low','High',
'Low','High','High','High','Low','Low','High','Low')
Loan_Given <- c('Y','N','N','Y','Y','Y','N','N','Y','N','Y','Y')
library(tidyverse)
df <- tibble(Income_Level,Loan_Given) %>%
group_by(Income_Level) %>%
mutate(
loan_lgl = case_when(
Loan_Given == "Y" ~ TRUE,
Loan_Given == "N" ~ FALSE
)
) %>%
summarise(pct_loan = 100 * mean(loan_lgl)) %>%
print()
#> # A tibble: 2 x 2
#> Income_Level pct_loan
#> <chr> <dbl>
#> 1 High 66.7
#> 2 Low 50.0