Skip to content
Prev 175333 / 398506 Next

Calculate directions between points

Thank you! I managed it with this function - for a data fram with x and y coordinates:

anglefun<-function(x,y,unit="degrees") {x.new<-outer(x,x,"-");
y.new<-outer(y,y,"-");angles<-atan2(x.new,y.new);diag(angles)<-NA;if(unit
== "degrees")
return(180*angles/pi)
if (unit == "radians")
return(angles)}

returns a matrix with the directions in degrees or radians and sets the angle between the same point to NA





Emmanuel Charpentier schrieb: 
Le samedi 28 mars 2009 ? 16:06 +0000, Wanja Mathar a ?crit :

Hi everyone,

i have a matrix of points and want to calculate the direction and length of each vector between the points.

I can easily calculate the length with dist(testdata), but is their a function that returns a matrix of the direction of the vectors between points (angles 0-360?)?

testdata <- matrix(1:8, nrow= ncol=2,
                dimnames=st(c("A","B","C","D"), c("x","y"))


?atan2 ; ?outer

					Emmanuel Charpentier