# Is there an R alternative for ArcMap's linear directional mean tool?

I'm writing an `R` package for trajectory analysis and need to determine the mean direction of lines. The only thing I found so far for this purpose is ArcMap's linear directional mean, but no alternatives. Is there any `R` implementation out there that maybe just goes under a different name?

Fairly easy to compute that LDM statistic for some line segments. Lets make some:

``````> lines = data.frame(x0=runif(10),y0=runif(10),x1=runif(10),y1=runif(10))
> lines
x0         y0         x1         y1
1  0.3562903 0.08386574 0.84049368 0.78357380
2  0.6793305 0.49920437 0.57025911 0.61850304
3  0.7314435 0.69672892 0.02226465 0.48937504
4  0.2728244 0.10763011 0.63589156 0.05583467
5  0.5035135 0.98126278 0.54574763 0.71125625
6  0.3694364 0.28743426 0.95582477 0.72959597
7  0.2692754 0.31636365 0.67497234 0.54334665
8  0.2259286 0.41579663 0.03271642 0.80521660
9  0.6290674 0.79029148 0.38631925 0.03286971
10 0.2945184 0.54637922 0.14816186 0.65597306
``````

Then compute the angle of each line segment by `atan2`:

``````> lines\$dx = lines\$x1-lines\$x0
> lines\$dy = lines\$y1-lines\$y0
> lines\$theta = atan2(lines\$dy, lines\$dx)
``````

Then get sines and cosines:

``````> lines\$sin = sin(lines\$theta)
> lines\$cos = cos(lines\$theta)
``````

Then the LDM as defined on that page is:

``````> atan2(sum(lines\$sin),sum(lines\$cos))
[1] 1.45913
``````

A few more lines gets you the quadrant connection in the second part.

You might find this in one of the packages for circular statistics in R.