Function to handle animal track data, organized as trip objects
Source: R/AllMethod.R
trip-methods.RdCreate an object of class trip, extending the basic functionality
of SpatialPointsDataFrame-class by specifying the data columns
that define the "TimeOrdered" quality of the records.
Usage
trip(obj, TORnames, correct_all = TRUE)
trip(obj) <- value
# S4 method for class 'trip,ANY'
split(x, f, drop = FALSE, ...)
# S4 method for class 'trip,ANY,ANY,ANY'
x[i, j, ..., drop = TRUE]Arguments
- obj
A data frame, a grouped data frame or a
SpatialPointsDataFrame-classcontaining at least two columns with the DateTime and ID data as perTORnames. See Details.- TORnames
Either a
TimeOrderedRecordsobject, or a 2-element character vector specifying the DateTime and ID column ofobj- correct_all
logical value, if
TRUEthe input data is corrected for common problems- value
A 4-element character vector specifying the X, Y, DateTime coordinates and ID of
obj.- x
trip object
- f
grouping vector as per
split()- drop
unused but necessary for method consistency
- i, j, ...
indices specifying elements to extract
Value
A trip object, with the usual slots of a
SpatialPointsDataFrame-class and the added
TimeOrderedRecords. For the most part this can be treated as a
data.frame with Spatial coordinates.
Details
The original form of trip() required very strict input as a 'SpatialPointsDataFrame' and
specifying which were the time and ID columns, but the input can be more flexible. If the object is a
grouped data frame ('dplyr-style') then the (first) grouping is assumed to define individual trips and that
columns 1, 2, 3 are the x-, y-, time-coordinates in that order. It can also be a trip object for
redefining TORnames.
The trip() function can ingest track_xyt, telemetry, SpatialPointsDataFrame, sf,
trackeRdata, grouped_df, data.frame, tbl_df, mousetrap, and in some cases
lists of those objects. Please get in touch if you think something that should work does not.
Track data often contains problems, with missing values in location or time,
times out of order or with duplicated times. The correct_all argument is
set to TRUE by default and will report any inconsistencies. Data really should
be checked first rather than relying on this auto-cleanup. The following problems are common:
duplicated records (every column with the same value in another row)
duplicated date-time values
missing date-time values, or missing x or y coordinates
records out of order within trip ID
For some data types there's no formal structure, but a simple convention such as
a set of names in a data frame. For example, the VTrack package has AATAMS1 which may be
turned into a trip with
trip(AATAMS1 %>% dplyr::select(longitude, latitude, timestamp, tag.ID, everything())
In time we can add support for all kinds of variants, detected by the names and contents.
See Chapter 2 of the trip thesis, doi:10.25959/23209538 , for more details.
Methods
Most of the methods available are by virtue of the sp package. Some, such
as split.data.frame have been added to SPDF so that trip has the same
functionality.
- trip
signature(obj="SpatialPointsDataFrame", TORnames="ANY")
The main construction.
- trip
signature(obj="SpatialPointsDataFrame", TORnames="TimeOrderedRecords")
Object and TimeOrdered records class
- trip
signature(obj="ANY", TORnames="TimeOrderedRecords"): create atripobject from a data frame.- trip
signature(obj="trip", TORnames="ANY"): (Re)-create atripobject using a character vector forTORnames.- trip
signature(obj="trip", TORnames="TimeOrderedRecords"): (re)-create a trip object using aTimeOrderedRecordsobject.
See also
speedfilter, and tripGrid for simplistic
speed filtering and spatial time spent gridding.
Examples
d <- data.frame(x=1:10, y=rnorm(10), tms=Sys.time() + 1:10, id=gl(2, 5))
## the simplest way to create a trip is by order of columns
trip(d)
#> Warning: input looks like longitude/latitude data, assuming +proj=longlat +datum=WGS84
#> coordinates tms id
#> 1 (1, 0.4703502) 2026-11-29 10:29:24.057498 1
#> 2 (2, 1.12128) 2026-11-29 10:29:25.057498 1
#> 3 (3, -0.05992252) 2026-11-29 10:29:26.057498 1
#> 4 (4, -0.9649836) 2026-11-29 10:29:27.057498 1
#> 5 (5, -0.04088883) 2026-11-29 10:29:28.057498 1
#> 6 (6, -0.1198719) 2026-11-29 10:29:29.057498 2
#> 7 (7, -0.3533248) 2026-11-29 10:29:30.057498 2
#> 8 (8, 0.6941524) 2026-11-29 10:29:31.057498 2
#> 9 (9, 0.3071144) 2026-11-29 10:29:32.057498 2
#> 10 (10, -0.03791189) 2026-11-29 10:29:33.057498 2
tr <- trip(d)
#> Warning: input looks like longitude/latitude data, assuming +proj=longlat +datum=WGS84
## real world data in CSV
mi_dat <- read.csv(system.file("extdata/MI_albatross_sub10.csv", package = "trip"),
stringsAsFactors = FALSE)
mi_dat$gmt <- as.POSIXct(mi_dat$gmt, tz = "UTC")
mi_dat$sp_id <- sprintf("%s%s_%s_%s", mi_dat$species,
substr(mi_dat$breeding_status, 1, 1), mi_dat$band, mi_dat$tag_ID)
sp::coordinates(mi_dat) <- c("lon", "lat")
## there are many warnings, but the outcome is fine
## (sp_id == 'WAi_14030938_2123' has < 3 locations as does LMi_12143650_14257)
mi_dat <- trip(mi_dat, c("gmt", "sp_id") )
#> Warning: ordering input records by trip ID, then time
#> Warning: removing trip IDs that have too few elements (<3):
#> 'LMi_12143650_14257,WAi_14030938_2123'
#> Warning: input looks like longitude/latitude data, assuming +proj=longlat +datum=WGS84
plot(mi_dat, pch = ".")
#lines(mi_dat) ## ugly
mi_dat_polar <- reproj(mi_dat, "+proj=stere +lat_0=-90 +lon_0=154 +datum=WGS84")
plot(mi_dat_polar, pch = ".")
lines(mi_dat_polar)