Calculate step length using R

trajectory analysis in r
trajectory modeling in r
r amt package tutorial
amt package r
trajr an r package for characterisation of animal trajectories
trajr github
calculate turning angle in r

I would like to know if there's a specific function in R that calculates step length, as being the distance (in meters) between two consecutive GPS time-registered locations.

I have a dataset looking as follows:

> head(datagps)
   Date & Time [Local]  Latitude Longitude
1: 2018-06-18 03:01:00 -2.434901  34.85359
2: 2018-06-18 03:06:00 -2.434598  34.85387
3: 2018-06-18 03:08:00 -2.434726  34.85382
4: 2018-06-18 03:12:00 -2.434816  34.85371
5: 2018-06-18 03:16:00 -2.434613  34.85372
6: 2018-06-18 03:20:00 -2.434511  34.85376

And would like to create a column Step which would do such operation as mentionned above. Maybe geosphere package has such function? If not, what would be the most compact way to do this?

Any input is appreciated!

You can use geosphere and calculate the distHaversine between two coordinates:

distm(c(lon1, lat1), c(lon2, lat2), fun = distHaversine)

Use mutate to add the Step field

[PDF] Package 'trajr', If not NULL, step angles are calculated relative to this angle (in radians), other- R rediscretization step length, in the spatial units of trj. Details. Stride length and step length are two important measurements in gait analysis. Gait analysis is a study of how a person walks and runs. Doctors use visual observation and tools to measure and

I vectorized the Haversine function found at

haversine <- function(lon1, lat1, lon2, lat2, r = 6378137) {

    if(!is.numeric(c(lon1, lat1, lon2, lat2)))
        stop("Inputs are not numeric")

    # Convert degrees to radians
    lon1 <- lon1 * pi / 180
    lat1 <- lat1 * pi / 180
    lon2 <- lon2 * pi / 180
    lat2 <- lat2 * pi / 180

    delta.lon <- (lon2 - lon1) <- (lat2 - lat1)
    a <- sin(^2 + cos(lat1) * cos(lat2) *
    c <- 2 * asin(min(1,sqrt(a)))
    d <- r * c

    return(d) # Distance

vectorized_haversine <- Vectorize(haversine, vectorize.args = c("lon1", "lat1", "lon2", "lat2"))

Next, I used the dplyr functions 'lag' and 'mutate', along with the vectorized haversine function to get the distances between consecutive points (the function 'tribble' was used to recreate head(datagps)).

      ~`Date & Time [Local]`, ~Latitude, ~Longitude,
       "2018-06-18 03:000", -2.434901,    34.85359,
       "2018-06-18 03:06:00", -2.434598,  34.85387,
      "2018-06-18 03:08:00", -2.434726,  34.85382,
       "2018-06-18 03:12:00", -2.434816,  34.85371,
      "2018-06-18 03:16:00", -2.434613,  34.85372,
       "2018-06-18 03:20:00", -2.434511,  34.85376
    ) %>% 
      mutate(Step = 
               vectorized_haversine(Longitude, Latitude, lag(Longitude), lag(Latitude)))

  Date & Time [Local]  Latitude Longitude     Step
1   2018-06-18 03:000 -2.434901  34.85359       NA
2 2018-06-18 03:06:00 -2.434598  34.85387 45.90731
3 2018-06-18 03:08:00 -2.434726  34.85382 15.29559
4 2018-06-18 03:12:00 -2.434816  34.85371 15.81292
5 2018-06-18 03:16:00 -2.434613  34.85372 22.62521
6 2018-06-18 03:20:00 -2.434511  34.85376 12.19500

[PDF] Package 'amt', culate track statistics (e.g. step lengths, speed, or turning angles), prepare calculated and in the second case the distance to the raster cell with the most relocations. r <- raster::raster(bbox(deer, buffer = 100), res = 40). 3. Calculate your step length. Calculate your step length by dividing the distance you walked by the number of steps you took. If you walked 12 steps in 30 feet, your step length is 2.5 feet. Here’s the math: 30 feet / 12 steps = 2.5 feet per step. 4. Program your step length into your pedometer.

Check out: gmapdistance

Note: A string or vector of strings containing the description of the starting point(s). Should be inside of quoutes (""). If more than one word for the same location is used, they should be separated by a plus sign e.g. "Bogota+Colombia". Coordinates in LAT-LONG format are also a valid input as long as they can be identified by Google Maps.

The function gmapsdistance uses the Google Maps Distance Matrix API in order to compute the distance(s) and time(s) between two points. In order to be able to use the function, you will need an API key and enable the Distance Matrix API in the Google Developers Console


Note that Google has changed its billing practices. (Joy)

You must have a valid API key and a billing account to access our APIs. When you enable billing, you will get $200 free usage every month for Maps, Routes, or Places. Based on the millions of users using our APIs today, most of them can continue to use Google Maps Platform for free with this credit. Having a billing account helps us understand our developers’ needs better and allows you to scale seamlessly.

For more information about how to get a key, go to

For more information about the Google Maps Distance Matrix API go to


A list with the traveling time(s) and distance(s) between origin(s) and destination(s) and the status

results = gmapsdistance(origin = "38.1621328+24.0029257", 
                        destination = "37.9908372+23.7383394",
                        mode = "walking")

steps: Functions to create and work with steps in amt: Animal , Description Usage Arguments Details Value Examples. View source: R/steps.R. Description. step_lengths can be use to calculate step lengths of a track. You may try nchar(c) function for other data types to find the length. Conclusion. In this R Tutorial, we have learned to use nchar(x) to find the string length or number length in R programming language.

Animal trajectory analysis with trajr, r can be calculated by calling Mod(TrajMeanVectorOfTurningAngles(trj)) , assuming trj is a Trajectory with constant step length. Within this section� After you calculate the variance of a set of numbers, you have a value whose units are different from your original measurements. For example, if your original measurements are in inches, their variance is in square inches. This is because you square the deviations before you average them. So the variance in the five-score population …

Animal movement tools (amt): R package for managing tracking data , By using common statistical distributions to model step length and turn angle Here, we present the R package amt (animal movement tools) that allows SSFs estimate conditional selection coefficients using a likelihood� Statistical analysis in R is performed by using many in-built functions. Most of these functions are part of the R base package. These functions take R vector as an input along with the arguments and give the result. The functions we are discussing in this chapter are mean, median and mode. Mean

[PDF] Animal Movement Tools (amt): R-Package for Managing , By using common statistical distributions to model step length and turn angle Here, we present the R-package amt (animal movement tools) that allows users Step-Selection Functions estimate conditional selection. One common type of problem that employs ratios may involve using ratios to scale up or down the two numbers in proportion to each other. Multiplying or dividing all terms in a ratio by the same number creates a ratio with the same proportions as the original, so, to scale your ratio, multiply or divide through the ratio by the scaling factor. [4]

  • Thank you for your answer. When using distm() how can I specify the c() arguments according to my syntax in the post? I'm using the following but I'm rather new to R so I'm totally unsure: > datagps$Step<-distm(c(datagps$Longitude, datagps$Latitude), c(datagps$Longitude+1, datagps$Latitude+1), fun = distHaversine) Error in .pointsToMatrix(x) : Wrong length for a vector, should be 2
  • I've used your script but getting the following error:> vectorized_haversine <- Vectorize(haversine, vectorize.args = c("lon1", "lat1", "lon2", "lat2")) > mutate(datagps$Step = Error: unexpected '=' in "mutate(datagps$Step =" > vectorized_haversine(datagps$Longitude, datagps$Latitude, lag(datagps$Longitude), lag(datagps$Latitude))) Error: unexpected ')' in " vectorized_haversine(datagps$Longitude, datagps$Latitude, lag(datagps$Longitude), lag(datagps$Latitude)))" Am I writing the script in a bad syntax or is there something else?
  • I'm not totally sure. The code I posted works for me. Did you change Step = to datagps$Step = inside of the mutate function? If so, just use Step =. Try: datagps_with_steps <- datagps %>% mutate(Step = vectorized_haversine(Longitude, Latitude, lag(Longitude), lag(Latitude)))