Re: [R] Question about merge()

From: Wensui Liu <liuwensui_at_gmail.com>
Date: Sun 24 Sep 2006 - 00:26:57 GMT

MERGE DATA FRAMES BY 2 OR MORE VARIABLES

#######################################
# MERGE 2 DATA FRAMES BASED ON        #
# 2 OR MORE VARIABLES                 #
#######################################

data1<-data.frame(x.id1 = 1:10, x.id2 = (1:10) * 2, x = rnorm(length(1:10)));
data2<-data.frame(y.id1 = seq(1, 20, by = 2), y.id2 = seq(1, 20, by = 2) * 2, y = rnorm(length(1:10)));

merged<-merge(data1, data2, by.x = c("x.id1", "x.id2"), by.y = c("y.id1", "y.id2"), all = TRUE);

On 9/23/06, Jonathan Greenberg <jgreenberg@arc.nasa.gov> wrote:
> If I want to do a join based on *two* matching fields in two data frames,
> can merge() handle this? It appears to only handle a single matching column
> -- do I need to make a "metacolumn" or is there some way to do this? E.g.:
>
> Dataframe 1 contains columns A,B,C and Dataframe 2 contains A,B,D
>
> I want an output A,B,C,D which places C and D together if A and B match
> (otherwise, make two new rows, e.g. Ax,Bx,Cx,nodata and Ay,By,nodata,Dy)
>
> --j
>
> --
> Jonathan A. Greenberg, PhD
> NRC Research Associate
> NASA Ames Research Center
> MS 242-4
> Moffett Field, CA 94035-1000
> Office: 650-604-5896
> Cell: 415-794-5043
> AIM: jgrn307
> MSN: jgrn307@hotmail.com
>
> ______________________________________________
> R-help@stat.math.ethz.ch 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.
>

-- 
WenSui Liu
(http://spaces.msn.com/statcompute/blog)
Senior Decision Support Analyst
Cincinnati Children Hospital Medical Center

______________________________________________
R-help@stat.math.ethz.ch 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.
Received on Sun Sep 24 10:31:21 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sun 24 Sep 2006 - 01:30:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.