TrajsMergeStats: Merge trajectory characteristics

View source: R/convenience.R

TrajsMergeStatsR Documentation

Merge trajectory characteristics

Description

Builds a data frame by combining rows of statistical values for multiple trajectories. The statistics for each trajectory are defined by the caller in a user defined function - see the example for one way to achieve this.

Usage

TrajsMergeStats(
  trjs,
  statsFn,
  progressBar = c("none", "text", "win", "tk"),
  check.names = TRUE,
  ...
)

Arguments

trjs

List of trajectories to be characterised.

statsFn

Function to calculate statistics of interest for a single trajectory.

progressBar

Displays an optional progressbar, which may be helpful if processing is very slow. The progressbar is displayed by printing to the console, by using winProgressBar or tkProgressBar, if progressBar is "text", "win" or "tk" respectively. The default is no progressbar (value "none"). The "win" progressbar is only available on Windows.

check.names

Passed to as.data.frame. Set to FALSE if statistic names are not syntactically valid variable names.

...

Additional arguments passed to statsFn.

Note

Any NULL valued statistics are converted to NAs.

Examples

## Not run: 

# Define a function which calculates some statistics
# of interest for a single trajectory
characteriseTrajectory <- function(trj) {
  # Measures of speed
  derivs <- TrajDerivatives(trj)
  mean_speed <- mean(derivs$speed)
  sd_speed <- sd(derivs$speed)

  # Resample to constant step length.
  # Step length must be appropriate for the trajectory
  resampled <- TrajRediscretize(trj, 2)

  # Measures of straightness
  sinuosity <- TrajSinuosity2(resampled)
  Emax <- TrajEmax(resampled)

  # Periodicity
  resampled <- TrajRediscretize(trj, .001)
  corr <- TrajDirectionAutocorrelations(resampled, round(nrow(resampled) / 4))
  first_min <- TrajDAFindFirstMinimum(corr)

  # Return a list with all of the statistics for this trajectory
  list(mean_speed = mean_speed,
       sd_speed = sd_speed,
       sinuosity = sinuosity,
       Emax = Emax,
       first_min_deltaS = first_min[1],
       first_min_C = first_min[2])
}

trjs <- TrajsBuild(filenames)
stats <- TrajsMergeStats(trjs, characteriseTrajectory)

## End(Not run)


JimMcL/trajr documentation built on July 23, 2024, 2:06 a.m.