2

I have a dataframe with columns as defined below. I have provided one set of example, similar to this I have many countries with loan amount and gender variables

      country          loan_amount          gender 
1      Austia              175                F        
2      Austia              100                F        
3      Austia              825                M        
4      Austia              175                F        
5      Austia             1025                M        
6      Austia              225                F        

Here I need to group by countries and then for each country, I need to calculate loan percentage by gender in new columns, so that new columns will have male percentage of total loan amount for that country and female percentage of total loan amount for that country. I need to do two group_by function, first to group all countries together and after that group genders to calculate loan percent.

    Total loan amount = 2525
     female_prcent = 175+100+175+225/2525 = 26.73
     male_percent = 825+1025/2525 = 73.26

The output should be as below:

      country            female_percent   male_percent 
1      Austia              26.73            73.26        

I am trying to do this in R. I tried the below function, but my R session is not producing any result and it is terminating.

  df %>%
  group_by(country, gender) %>%
  summarise_each(funs(sum))

Could someone help me in achieving this output? I think this can be achieved using dplyr function, but I am struck inbetween.

SRS
  • 439
  • 9
  • 19
  • As there is only one column, you can use `summarise` instead of summarise_each` – akrun Jun 05 '16 at 14:51
  • You mean to summarise the sum of loan amount column? – SRS Jun 05 '16 at 14:53
  • Yes, it should work, but I am not sure what you meant by terminating? – akrun Jun 05 '16 at 14:54
  • R studio is getting aborted. I tried summarise option, but it is not helping out. – SRS Jun 05 '16 at 14:57
  • As @akrun said, you can just use `summarise`. E.g.: `d1 %>% group_by(country, gender) %>% summarise(amt = sum(loan_amount)) %>% transmute(gender = gender, perc = amt/sum(amt))` will give the desired result in long format. – Jaap Jun 05 '16 at 15:02
  • This looks like a group by country only. `df %>% group_by(country) %>% summarise(female_percent= sum(loan_amount[gender=="F"])/sum(loan_amount), male_percent = sum(loan_amount[gender=="M"])/sum(loan_amount))`. – Pierre L Jun 05 '16 at 15:43
  • Thanks a lot for all your suggestions. – SRS Jun 05 '16 at 16:21

1 Answers1

1

We can try the weighted table from questionr package:

library(questionr)
with(df, wtd.table(country, gender, weights = round(100 * loan_amount/sum(loan_amount), 2)))

           F     M
Austia 26.73 73.26
Psidom
  • 209,562
  • 33
  • 339
  • 356