Dear Lister, I understand merge() can be used to join 2 data frames based on 1 variable. But how about merge based on more than 2 variables? Thank you so much! -- WenSui Liu (http://spaces.msn.com/statcompute/blog) Senior Decision Support Analyst Health Policy and Clinical Effectiveness Cincinnati Children Hospital Medical Center [[alternative HTML version deleted]]
Simon Blomberg
2006-Aug-15 03:05 UTC
[R] merge 2 data frame based on more than 2 variables
Wensui Liu wrote:> Dear Lister, > > I understand merge() can be used to join 2 data frames based on 1 variable. > But how about merge based on more than 2 variables? > > Thank you so much! > >Just specify the 2 (or more) variable names in a column vector for "by") merge(dat1, dat2, by= c("VarA", "VarB")) assuming both data frames have columns VarA and VarB. -- Simon Blomberg, B.Sc.(Hons.), Ph.D, M.App.Stat. Centre for Resource and Environmental Studies The Australian National University Canberra ACT 0200 Australia T: +61 2 6125 7800 email: Simon.Blomberg_at_anu.edu.au F: +61 2 6125 0757 CRICOS Provider # 00120C
what if the names are different in 2 data frames? On 8/14/06, Simon Blomberg <blomsp at ozemail.com.au> wrote:> Wensui Liu wrote: > > Dear Lister, > > > > I understand merge() can be used to join 2 data frames based on 1 variable. > > But how about merge based on more than 2 variables? > > > > Thank you so much! > > > > > Just specify the 2 (or more) variable names in a column vector for "by") > > merge(dat1, dat2, by= c("VarA", "VarB")) > > assuming both data frames have columns VarA and VarB. > > -- > Simon Blomberg, B.Sc.(Hons.), Ph.D, M.App.Stat. > Centre for Resource and Environmental Studies > The Australian National University > Canberra ACT 0200 > Australia > T: +61 2 6125 7800 email: Simon.Blomberg_at_anu.edu.au > F: +61 2 6125 0757 > CRICOS Provider # 00120C > >-- WenSui Liu (http://spaces.msn.com/statcompute/blog) Senior Decision Support Analyst Health Policy and Clinical Effectiveness Cincinnati Children Hospital Medical Center
Simon Blomberg
2006-Aug-15 03:54 UTC
[R] merge 2 data frame based on more than 2 variables
Then instead of by, use by.x and by.y to specifiy the variable names separately for both data frames. See ?merge, especially the examples. Wensui Liu wrote:> what if the names are different in 2 data frames? > > > On 8/14/06, Simon Blomberg <blomsp at ozemail.com.au> wrote: > >> Wensui Liu wrote: >> >>> Dear Lister, >>> >>> I understand merge() can be used to join 2 data frames based on 1 variable. >>> But how about merge based on more than 2 variables? >>> >>> Thank you so much! >>> >>> >>> >> Just specify the 2 (or more) variable names in a column vector for "by") >> >> merge(dat1, dat2, by= c("VarA", "VarB")) >> >> assuming both data frames have columns VarA and VarB. >> >> -- >> Simon Blomberg, B.Sc.(Hons.), Ph.D, M.App.Stat. >> Centre for Resource and Environmental Studies >> The Australian National University >> Canberra ACT 0200 >> Australia >> T: +61 2 6125 7800 email: Simon.Blomberg_at_anu.edu.au >> F: +61 2 6125 0757 >> CRICOS Provider # 00120C >> >> >> > > >-- Simon Blomberg, B.Sc.(Hons.), Ph.D, M.App.Stat. Centre for Resource and Environmental Studies The Australian National University Canberra ACT 0200 Australia T: +61 2 6125 7800 email: Simon.Blomberg_at_anu.edu.au F: +61 2 6125 0757 CRICOS Provider # 00120C