Search code examples
rdatabasevariablesvar

count houshold income using R


I have this data frame where I should count for each household how much monetary support they should get;

My data looks like this:

hh_id gender hh_income
1 F 563
1 H 563
1 H 563
2 F 0
2 F 0
2 F 0
3 H 0
3 F 0

Those with the same hh_id are from the same household;

If hh_icome == 0; I should count how many people are in that household and multiply that number by 175$ and that is the hh_income for that hh.

Example: the hh with the Id == 2 has the income of 0; have 3 individuals so The income should change from 0 to 3*175=525; so the data for that he would look like this:

hh_id gender hh_income
2 F 525
2 F 525
2 F 525

any ideas please;

best regards


Solution

  • library(dplyr)
    
    df %>% 
      group_by(hh_id) %>% 
      mutate(hh_income = case_when(hh_income == 0 ~ n() * 175, 
                                   TRUE ~ hh_income))
    
    # A tibble: 8 x 3
    # Groups:   hh_id [3]
      hh_id gender hh_income
      <dbl> <chr>      <dbl>
    1     1 F            563
    2     1 H            563
    3     1 H            563
    4     2 F            525
    5     2 F            525
    6     2 F            525
    7     3 H            350
    8     3 F            350