Chandrasen
Chandrasen

Reputation: 11

how to find the percentage of credit card holders for different age groups

The ACCOUNT table contains a list of accounts held by customers. A customer may have more than one account of each type The layout for the table is as follows:

CUSTOMER_NUMBER CUSTOMER_AGE    ACCOUNT_NUMBER      ACCOUNT TYPE
123             27              A987                Home Loan
123             27              B6547               Credit Card
124             42              B7531               Credit Card

Show how you determine the proportion of customers with a credit card for each of the following age ranges : 18 – 29, 30 – 44, 45 – 59, 60+.

The output should look something like this:

Age Band    % with Credit Card
18-29   44.9%
30-44   41.2%
45-59   45.5%
60+ 43.0%

How do I get the expected result as shown in above table with percentage of different age groups , which are those who holds credit card.

Please help me in R code

Upvotes: 1

Views: 43

Answers (1)

tmfmnk
tmfmnk

Reputation: 40141

One dplyr possibility could be:

df %>%
 group_by(grp = cut(CUSTOMER_AGE,
                  breaks = c(18, 29, 44, 59, Inf),
                  labels = c("18-29", "30-44", "45-59", "60+"),
                  right = FALSE)) %>%
 summarise(res = (length(ACCOUNT_TYPE[ACCOUNT_TYPE == "Credit_Card"])/n()*100))

  grp     res
  <fct> <dbl>
1 18-29    50
2 30-44   100

Sample data:

df <- read.table(text = "CUSTOMER_NUMBER CUSTOMER_AGE    ACCOUNT_NUMBER      ACCOUNT_TYPE
123             27              A987                Home_Loan
                 123             27              B6547               Credit_Card
                 124             42              B7531               Credit_Card", header = TRUE,
                 stringsAsFactors = FALSE)

Upvotes: 0

Related Questions