Rainbow Deals - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Creating functions
If statements
Summary
20. Exercise 2

Instruction

The two data frames contain data per branch (city), and each branch is within a specific country. In some cases, Solbank has multiple branches within a country. Suppose Solbank is interested in identifying the country where it has the greatest presence.

Exercise

Create a function named KPI_per_country that takes two arguments: df and country. The first argument, df, represents the data frame we're using (in our case, either bank_branches_2017 or bank_branches_2018). The second argument represent the name of the country in question (for which we are doing a total count).

The function should return a data frame that has three columns: country (the country for which we are calculating totals), clients, and accounts. Those last two columns should be totals for the given country. Those totals are calculated using the number_of_clients and opened_accounts columns from the given data frame, summed across all branches for the given country.

Here's an example: If we call our function with df = bank_branches_2018 and country = "Austria", one row should be returned:

country clients accounts
1 Austria 26745 34850

Stuck? Here's a hint!

Hint 1:

df[df$country == country, "number_of_clients"] will give you the number of clients for a specific country.
df[df$country == country, "opened_accounts"] will give you the number of opened accounts for those clients.

Hint 2: use the data.frame() function. In case you don't remember how to use it, here's an example:

data.frame(
    country = country, 
    clients = sum(df[df$country == country, "number_of_clients"]), 
    accounts = sum(df[df$country == country, "opened_accounts"])
  )