[R] calc dist AB for B at levels of A

Emmanuel Charpentier charpent at bacbuc.dyndns.org
Thu Nov 15 00:41:56 CET 2007


Melanie Ann Harsch a écrit :
> I have a question that I have been trying to figure out and I imagine 
> there is a very simple answer to it.
> 
> I am trying to use the distAB function in the clim.pact package
> I have a dataframe with 4 columns in which A is ref pt and B is site
> 1. longitude of A
> 2. Latitude of A
> 3. longitude of B
> 4. Latitude of B
> 
> Problem is the columns are unequal in length 
> column 1 and 2 are length=29 and 3 and 4 length=53

Curious representation... I'd rather use two dataframes (those are
different sets, of course ?

> What I want to do is create a matrix which are filled with values of 
> distAB for B (site) at all levels of A (reference points)
> 
> Any ideas?

OTTOMM :

D1<-df[1:29,1:3]
D2<-df[,3:4]
t<-outer(1:29,1:53,FUN=function(i,j)distAB(D1[i,1],D1[i,2],D2[j,1],D2[j,2]))

This is probably the most straightforward, and might be the more
efficient (no loops). However, you'll note that, if one set is a subset
of the other, each distance between points of the inner set is computed
*twice*. If the 'cost" of computing a distance is much higher than the
cost of an ifelse(), treating this special case might be worthy (but
that's not easy).

HTH,

					Emmanuel Charpentier



More information about the R-help mailing list