[R] Combining data.frames

Jeff Reichman re|chm@nj @end|ng |rom @bcg|ob@|@net
Sun Mar 20 02:15:34 CET 2022


R-Help Community

I'm trying to combine two data.frames which each containing 10 columns of
which they each share two common fields. Here are two small test datasets.

df1 <- data.frame(date =
c("2021-1-1","2021-1-1","2021-1-1","2021-1-1","2021-1-1",
 
"2021-1-2","2021-1-2","2021-1-3","2021-1-3","2021-1-3"),
                  geo_hash =
c("abc123","abc123","abc456","abc789","abc246","abc123",
                               "asd123","abc789","abc890","abc123"),
                  ad_id = c("a12345","b12345","a12345","a12345","c12345",
                            "b12345","b12345","a12345","b12345","a12345"))
df2 <- data.frame(date =
c("2021-1-1","2021-1-1","2021-1-2","2021-1-3","2021-1-3"),
                  geo_hash =
c("abc123","abc456","abc123","abc789","abc890"),
                  event = c("shoting","ied","protest","riot","protest"))

I'm trying to combine them such that I get a combined data.frames such as 

date		geo_hash	ad_id		event
1/1/2021	abc123		a12345		shoting
1/1/2021	abc123		b12345	
1/1/2021	abc456		a12345		ied
1/1/2021	abc789		a12345	
1/1/2021	abc246		c12345	

Jeff



More information about the R-help mailing list