jeudi 19 novembre 2015

Double match in r

I have a huge data set in r with one row per individual. One of my columns shows a family identifier (note, sex==1, male, sex==2, female).

ind sex income  hw  family.id
1   1    10     6    fam.1
2   2    8      7    fam.1
3   2    15     8    fam.2
4   1    7      4    fam.3
5   2    9      5    fam.3

How can I do a "double matching" so I can match couples in the data set for many of the variables that I am interested? For example, let's say individual 2, female, married with individual 1, male, should receive an entry in a new column with his income (same goes for hw):

ind sex income  hw  family.id  income.male   hw.male
1   1    10     6    fam.1       10            6
2   2    8      7    fam.1       8             6
3   2    15     8    fam.2       -             -
4   1    7      4    fam.3       7             7  
5   2    9      5    fam.3       9             7

I've said "double matching" in the title because I don't need to match only the family.ID, but I need to find a male that matches this fam.id. The reason I am doing this is because later all males will be dropped from the data set and I will remain only with rows for females.

I am sorry I can't show any coding I've worked. I've tried many approaches using match, ifelse, lapply and even unlist but it is not worth to add it here as unfortunately I can't make it work.

Anyone has a clue? We can work with both data.frames or data.tables environments.

Aucun commentaire:

Enregistrer un commentaire