Dear R-users, i have a simple problem maybe, but i don't see the solution. i want to find the entry-wise closest element of an vector compared with another. ind1<-c(1,4,10) ind2<-c(3,5,11) for (i in length(ind2):1) { print(which.min(abs(ind1-ind2[i]))) } for ind2[3] it should be ind1[3] 10, for ind2[2] it should be ind1[2] 4 and for ind2[1] it should be ind1[1] 1. but with the for-loop above i get ind1[3], ind1[2] and ind1[2]. any suggestions are quite welcome. best regards Andreas
On Jan 17, 2010, at 11:00 AM, Andreas Wittmann wrote:> Dear R-users, > > i have a simple problem maybe, but i don't see the solution. i want > to find the entry-wise closest element of an vector compared with > another. > > ind1<-c(1,4,10) > ind2<-c(3,5,11) > > for (i in length(ind2):1) > { > print(which.min(abs(ind1-ind2[i]))) > } > > for ind2[3] it should be ind1[3] 10, for ind2[2] it should be > ind1[2] 4 and for ind2[1] it should be ind1[1] 1. but with the for- > loop above i get ind1[3], ind1[2] and ind1[2]. > > any suggestions are quite welcome.You are failing to use the indices that which.min is providing. (And I think the closest in ind1 to ind2[1] is not 1, but rather 4, so see if this looks more responsive to your expectations: > for (i in length(ind2):1) + { + print( ind1[which.min(abs(ind1-ind2[i]))] ) + } [1] 10 [1] 4 [1] 4> > best regards > > Andreas > > ______________________________________________ > 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.David Winsemius, MD Heritage Laboratories West Hartford, CT
Is it not giving you the location of the minimum value not the value? See ind1<-c(1,4,10) ind2<-c(3,5,11) i=3 (ind1-ind2[i]) (aa <- abs(ind1-ind2[i])) which.min(aa) --- On Sun, 1/17/10, Andreas Wittmann <andreas_wittmann at gmx.de> wrote:> From: Andreas Wittmann <andreas_wittmann at gmx.de> > Subject: [R] enty-wise closest element > To: r-help at r-project.org > Received: Sunday, January 17, 2010, 11:00 AM > Dear R-users, > > i have a simple problem maybe, but i don't see the > solution. i want to find the entry-wise closest element of > an vector compared with another. > > ind1<-c(1,4,10) > ind2<-c(3,5,11) > > for (i in length(ind2):1) > { >? print(which.min(abs(ind1-ind2[i]))) > } > > for ind2[3] it should be ind1[3] 10, for ind2[2] it should > be ind1[2] 4 and for ind2[1] it should be ind1[1] 1. but > with the for-loop above i get ind1[3], ind1[2] and ind1[2]. > > any suggestions are quite welcome. > > best regards > > Andreas > > ______________________________________________ > 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. >__________________________________________________________________ Get a sneak peak at messages with a handy reading pane with All new Yahoo! Mail: http://ca.promos.yahoo.com/newmail/overview2/
Here is one approach if I understand your requirements correctly. ind1<-c(1,4,10) ind2<-c(3,5,11) m <- expand.grid(ind2=ind2,ind1=ind1) m$diff <- (m$ind2 - m$ind1) f <- function(x) { min_idx <- which.min(x$diff[x$diff > 0 & x$diff < x$ind2]) list(c(elem=unique(x$ind2),min.value=x$ind1[min_idx],min.index=min_idx)) } by(m, factor(m$ind2), f) This yields the output: elem min.value min.index 3 1 1 ------------------------------------------------------------ elem min.value min.index 5 4 2 ------------------------------------------------------------ elem min.value min.index 11 10 3