How to group by multiple columns in dataframe using R and do aggregate function
library(dplyr)
library(tidyr)     
df %&gt;% group_by(country, gender) %&gt;% 
       summarise(total_loan_amount =sum(loan_amount)) %&gt;% 
       spread(gender, total_loan_amount) %&gt;% 
       ungroup() %&gt;%
       transmute(country = country,  female_percent = F / (F+M), male_percent = M /(F+M))


results in

 Source: local data frame [1 x 3]

      country female_percent male_percent
       (fctr)          (dbl)        (dbl)
    1  Austia      0.2673267    0.7326733