Hi there, I'm new working with R and I am trying to find the unchosen set of islands that are closer to all the possible combination (of 2 out of 13) of a set of chosen islands. In other words, if I have a set of 13 islands (1, 2, ...13) and choose all the possible combination of 2 islands (1-2, 1-3,...12-13) for each chosen pair of islands which of the unchosen islands are the closest.
My code is below, but the problem is the loops seem not to be working library(gtools) library(session n=length(a) closeout=matrix(NA,nrow=78, ncol=2) for (i in 2){ x<-combinations(13,i) #considering these i islands others=matrix(nrow=78, ncol=2) for (j in 1:78){ #for all combinations of i islands acopy=a #make a copy of a for manipulation a<-as.matrix(a) #finding out which islands are selected and removing them from consideration others=paste('(acopy==',x[j,1],sep='') for(islands in 1:2){ others=paste(others,' |acopy==',x[j,islands],sep='') others=paste(others,')',sep='') } acopy[eval(parse(text=others))]=NA #loop through each island in this set of islands and get closest island, but exclude islands already counted for(k in 1:i){ closeout[i,1]=a[x[j,],is.finite(as.numeric(a[x[j,k],]))][1] closeout[i,2]=a[x[j,k],is.finite(as.numeric(a[x[j,k],]))][1] acopy[a==closeout[i,]]=NA } } write.csv(closeout, paste('res_closest',i,sep='','.csv')) } ################# The data of which island is closer to which is: a V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 [1,] 8 2 4 9 13 3 11 12 10 5 6 7 [2,] 1 8 4 9 13 3 11 12 10 5 6 7 [3,] 13 11 9 12 5 10 4 6 7 8 1 2 [4,] 9 8 13 3 11 1 12 10 5 2 6 7 [5,] 12 6 3 10 11 7 13 9 4 8 1 2 [6,] 7 10 5 12 11 3 13 9 4 8 1 2 [7,] 6 10 5 12 11 3 13 9 4 8 1 2 [8,] 1 4 2 9 13 3 11 12 10 5 6 7 [9,] 13 3 4 11 12 10 5 8 6 1 7 2 [10,] 12 11 13 6 5 3 7 9 4 8 1 2 [11,] 13 12 10 3 9 5 6 4 7 8 1 2 [12,] 10 11 13 3 5 6 9 7 4 8 1 2 [13,] 9 11 3 12 10 4 5 6 7 8 1 2 Each row is an island and column V1 is the closest island to the islands, V2 the 2nd closest island....V12 is the farthest island. Any help provided would be greatly appreciated, Rocio -- Rocio Ponce-Reyes PhD Candidate The Ecology Center University of Queensland St. Lucia QLD 4072 [[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.