
So, given these vectors, what's the right answer? 

a <-c(1,2,3) 
b<-c(2, 3, 4 ,5, 6) 

2,2,3 (distance =1) or 2,3,4 (distance=3)? 

I.e., is recycling of elements allowed or must the pairing be one-one? 

if the first, then: 

df <- expand.grid(a,b) 
df$dist <- with(df, (Var1-Var2)^2 ) 
head(df[order(df$dist),], min(length(a),length(b)) ) 

Var1 Var2 dist 
2 2 2 0 
6 3 3 0 
1 1 2 1 
-- 
David Winsemius 
----- Original Message ----- 
From: "Thomas S. Dye" <tsd@tsdye.com> 
To: r-help@r-project.org 
Sent: Tuesday, May 12, 2009 10:02:29 AM GMT -08:00 US/Canada Pacific 
Subject: [R] pair matching 

Given two numeric vectors of possibly unequal length, I'd like to pair 
each element of the shorter vector with an element of the longer 
vector such that the sum of squared differences between the pairs is 
minimized. Can someone point me to an R function or an algorithm for 
accomplishing this? 

All the best, 
Tom 

Thomas S. Dye, Ph.D. 
T. S. Dye & Colleagues, Archaeologists, Inc. 
Phone: (808) 529-0866 Fax: (808) 529-0884 
http://www.tsdye.com 



[[alternative HTML version deleted]] 

______________________________________________ 
R-help@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. 

	[[alternative HTML version deleted]]


