Hi. I have a data frame with 2 variables (x & y). For each row(i) I need to find an alternate row(j) with similar x & y by sub-setting such that 0.5x[i] <= x(j) <= 1.5x[i], and then choosing the minimum y[i]-y[j] from that subset. I am currently implementing this using a for loop:
test<-data.frame(x<-runif(100),y<-runif(100)) for (i in 1:length(test$x)){ test$z[i]<-which(test$y[which(test$x[-i]>0.5*test$x[i] & test$x[-i]<1.5*test$x[i])]-test$y[i] == min(test$y[which(test$x[-i]>0.5*test$x[i] & test$x[-i]<1.5*test$x[i])]-test$y[i])) } This gives me a column of row numbers corresponding to the desired observation. However, my actual data is much larger (about 50,000 rows) and is only a small part of my overall code. Is anyone aware of a way of doing this without resorting to a for loop? Thanks. -- View this message in context: http://old.nabble.com/finding-an-observation-with-similar-characteristics-tp26194391p26194391.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.