[R] Merging nested files

David Kaplan dkaplan at education.wisc.edu
Sun Nov 2 19:49:16 CET 2008

I think this will work.  Thanks.  But, is there a command to merge on a 
string variable.  All I have is the country 3 letter designation.

Thanks again,


David Kaplan, Ph.D.
Department of Educational Psychology
University of Wisconsin - Madison
Educational Sciences, Room, 1061
1025 W. Johnson Street
Madison, WI 53706

email: dkaplan at education.wisc.edu
Phone: 608-262-0836

Erik Iverson wrote:
> 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