Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

creating a variable based on other factors using R

My data looks like this:

hh_id indl ind_salary hh_income
1 1 200
1 2 450
1 3 00
2 4 1232
2 5 423

Individuals with the same hh_id lives in the same household so they will have the same household income. And for that the variable hh_income equal the sum of the salary of all persons with the same hh_id;

so my data would look like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

hh_id indl ind_salary hh_income
1 1 200 650
1 2 450 650
1 3 00 650
2 4 1232 1655
2 5 423 1655

Any ideas please;

>Solution :

You can use R base function ave to generate sum of ind_salary grouped by hh_id and get a vector of the same length of ind_salary

> df$hh_income <- ave(df$ind_salary, df$hh_id, FUN=sum)
> df
  hh_id indl ind_salary hh_income
1     1    1        200       650
2     1    2        450       650
3     1    3          0       650
4     2    4       1232      1655
5     2    5        423      1655
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading