[R] Merging nested files

Erik Iverson iverson at biostat.wisc.edu
Sun Nov 2 18:24:28 CET 2008


Hello -

David Kaplan wrote:
> Greetings all,
> 
> I have three files that I would like to merge.  The first is a student 
> level file that contains the student ID, their school ID, and their 
> country ID.
> 
> The second is the school file that contains the school ID and country ID.
> 
> The third is the country file with a country ID.
> 
> I would like to merge the three together using the common country ID.  
> Of course, what this would mean is that school data be repeated for 
> every student in their school, and country data repeated for every 
> school and student in that country.
> Any direction would be helpful.


I believe what you are after is to use the merge function.  'merge' 
takes two data.frames, so to merge 3 data.frames, you'll have to use it 
twice.  You may want the all = TRUE argument to merge also.

Example, with your data.frames, it might look like:

tmp <- merge(student, school, all = TRUE)
df  <- merge(tmp, country, all = TRUE)

One thing to watch out for is that the merge function will merge on the 
common names in each data.frame, in your case country ID.  Sometimes I 
check before the merge what variables it will attempt to merge on by using

intersect(names(student), names(school))

and making sure it is what I expect, i.e. ususally only one common name.

Hope that helps.

> 
> Thanks in advance,
> 
> David
> 
>



More information about the R-help mailing list