Re: [R] enty-wise closest element

2010-01-18 Thread Jason Smith
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$i

Re: [R] enty-wise closest element

2010-01-17 Thread Charles C. Berry
On Sun, 17 Jan 2010, Andreas Wittmann wrote: Thank you very much for your quick answers. Sorry, but i forgot to explain i want to find the closest and smaller element for each entry, so ind1[3] is smaller as 11 and close to 11, ind1[2] is smaller as 5 and close to 5 and ind1[1] is smaller tha

Re: [R] enty-wise closest element

2010-01-17 Thread David Winsemius
On Jan 17, 2010, at 12:30 PM, Andreas Wittmann wrote: Thank you very much for your quick answers. Sorry, but i forgot to explain i want to find the closest and smaller element for each entry, so ind1[3] is smaller as 11 and close to 11, ind1[2] is smaller as 5 and close to 5 and ind1[1] is

Re: [R] enty-wise closest element

2010-01-17 Thread Andreas Wittmann
Thank you very much for your quick answers. Sorry, but i forgot to explain i want to find the closest and smaller element for each entry, so ind1[3] is smaller as 11 and close to 11, ind1[2] is smaller as 5 and close to 5 and ind1[1] is smaller than 3 and close to 3. best regards Andreas

Re: [R] enty-wise closest element

2010-01-17 Thread John Kane
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 wrote: > From: Andreas Wittmann > Subject: [R] enty-wise closest element

Re: [R] enty-wise closest element

2010-01-17 Thread David Winsemius
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(

[R] enty-wise closest element

2010-01-17 Thread Andreas Wittmann
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,