[R] Matching data to a new column

Dieter Menne dieter.menne at menne-biomed.de
Fri Jun 26 13:04:31 CEST 2009


Kubasiewicz, Laura <laura.kubasiewicz08 <at> imperial.ac.uk> writes:

> I have a dataframe with columns for...
> 
> 'I.D'  'age'  'mothers I.D'
> 01     5       03
> 02     6       06
> 03     16     NA
> 04     8       06
> 05     3       NA
> 06     17     NA
> 
> I need to create a new column for 'mothers age' which puts the age of the
individual with 'mothers i.d' into
> the row for her offspring (so individual 01 would have 16 in the mothers age
column, as thats the age of
> individual 03)
> 

That's at typical join in SQL language, and it is handled in R with function
merge. In your special case, you would merge the dataframe with itself, 
so something like

merge(fr,fr, by... and check the NA cases)

Dieter




More information about the R-help mailing list