0

I have a data frame which includes the movement path of an animal in lat/lon coordinates.

From this I have derived speed and bearing over ground. The data frame also contains the wind vector relative to the animal's position i.e. speed and bearing of the wind relative to the animal at time t.

I would like to use vector addition to calculate the movement of the animal through the air (rather than relative to the ground) so as to calculate the movement vector the animal would have to follow in order to produce it's geo-referenced track given the wind conditions.

Here is some example data:

        lat       lon  speed.geo   dir.geo speed.flow   dir.flow
1  58.65417 -3.179046        NaN   0.00000  0.4308415 151.865239
2  58.65483 -3.180403 0.44041631 313.16209  0.4308415 151.865239
3  58.66102 -3.187734 0.05788974 328.34835  0.1310305  95.664332
4  58.66409 -3.190197 0.15473413 337.35945  0.2018593 150.394597
5  58.67433 -3.182058 0.04170939  22.44943  0.1635966  21.383810
6  58.67475 -3.181318 0.24299875  42.15219  0.1635966  21.383810
7  58.67520 -3.181994 0.21488370 322.13387  0.1635966  21.383810
8  58.67472 -3.181549 0.20505462 154.49798  0.1635966  21.383810
9  58.67325 -3.178166 1.11257613 129.96324  0.1379763   4.478803
10 58.67234 -3.177124 0.45663637 149.03759  0.1357501 357.369873

and here it is in a dput:

structure(list(lat = c(6501.55844075436, 6501.63236163928, 6502.32219361407, 
6502.66453486307, 6503.80350448795, 6503.85070971918, 6503.90114368413, 
6503.84707158286, 6503.68242346453, 6503.5817192008), lon = c(489.610443265389, 
489.531898544151, 489.108436319722, 488.966512812528, 489.441757188732, 
489.484800489562, 489.445733748482, 489.471394150017, 489.667148867539, 
489.727314726641), speed.geo = c(NaN, 0.440416314391926, 0.0578897406018802, 
0.154734132490158, 0.0417093915733849, 0.242998750785266, 0.214883703663591, 
0.205054624380294, 1.11257613127184, 0.45663636779519), dir.geo = c(0, 
313.1620935132, 328.348350423236, 337.359451215582, 22.4494313495932, 
42.1521946548759, 322.133869730753, 154.497980751214, 129.963235575701, 
149.037589410303), speed.flow = c(0.430841479520486, 0.430841479520486, 
0.131030528451571, 0.201859265857843, 0.16359657788002, 0.16359657788002, 
0.16359657788002, 0.16359657788002, 0.137976282783002, 0.135750095569998
), dir.flow = c(151.865238547752, 151.865238547752, 95.6643315716291, 
150.394596637891, 21.3838095217828, 21.3838095217828, 21.3838095217828, 
21.3838095217828, 4.47880272242145, 357.369872864835)), .Names = c("lat", 
"lon", "speed.geo", "dir.geo", "speed.flow", "dir.flow"), row.names = c(NA, 
-10L), class = "data.frame")

The expected output should be 2 further columns in the data frame representing the vector of animal movement relative to air flow (i.e. its flight speed and direction). It is in effect basic triganometry but I cannot compute it in R...

Jojo
  • 4,951
  • 7
  • 23
  • 27

1 Answers1

1

To sum vectors, you need then to be on a Cartesian system (not polar). So, you should convert then to a x-y component system and sum.

One other way to do what you want is to use a complex number to represent the speed and its direction (like x being real, and y, imaginary).
The following code converts your speed values to complex number, sum them, and extract the modulus (speed) and argument (dir). The direction is converted to degrees again.

library(dplyr)
df<-mutate(df,
       complex.geo = complex(modulus = speed.geo, 
                             argument = dir.geo/360*2*pi),
       complex.flow = complex(modulus = speed.flow,
                              argument = (dir.flow/360*2*pi)),
       complex.relat = complex.geo-complex.flow,
       speed.relat = Mod(complex.relat),
       dir.relat = ifelse(Arg(complex.relat)*360/(2*pi)<0,
                          Arg(complex.relat)*360/(2*pi)+360,
                          Arg(complex.relat)*360/(2*pi))) %>%
select(lat,lon,speed.geo,dir.geo,speed.flow,dir.flow,speed.relat,dir.relat)
André Costa
  • 377
  • 1
  • 11