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. Thanks in advance, David -- ======================================================================David Kaplan, Ph.D. Professor 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 homepage: http://www.education.wisc.edu/edpsych/default.aspx?content=kaplan.html Phone: 608-262-0836
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 > >
Read them into data frames and then use the R sqldf package: http://sqldf.googlecode.com On Sun, Nov 2, 2008 at 12:04 PM, David Kaplan <dkaplan at education.wisc.edu> 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. > > Thanks in advance, > > David > > > -- > ======================================================================> David Kaplan, Ph.D. > Professor > 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 > homepage: > http://www.education.wisc.edu/edpsych/default.aspx?content=kaplan.html > Phone: 608-262-0836 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >