Learn R Programming

trip (version 1.5.0)

trip-methods: Function to handle animal track data, organized as "trip"s

Description

Create an object of class "trip", extending the basic functionality of SpatialPointsDataFrame by specifying the data columns that define the "TimeOrdered" quality of the records.

Usage

trip(obj, TORnames)

trip(obj) <- value

# S4 method for trip,ANY,ANY,ANY [(x, i, j, ..., drop = TRUE)

Arguments

obj

A SpatialPointsDataFrame, or an object that can be coerced to one, containing at least two columns with the DateTime and ID data as per TORnames. It can also be a trip object for redefining TORnames.

TORnames

Either a TimeOrderedRecords object, or a 2-element character vector specifying the DateTime and ID column of obj

value

A 4-element character vector specifying the X, Y, DateTime coordinates and ID of obj.

x

trip object

i, j, …

indices specifying elements to extract

drop

unused but necessary for method consistency

Value

A trip object, with the usual slots of a SpatialPointsDataFrame and the added TimeOrderedRecords. For the most part this can be treated as a data.frame with Spatial coordinates.

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="ANY", TORnames="TimeOrderedRecords"): create a trip object from a data frame.

trip

signature(obj="trip", TORnames="ANY"): (Re)-create a trip object using a character vector for TORnames.

trip

signature(obj="trip", TORnames="TimeOrderedRecords"): (re)-create a trip object using a TimeOrderedRecords object.

See Also

speedfilter, and tripGrid for simplistic speed filtering and spatial time spent gridding.

Examples

Run this code
# NOT RUN {

d <- data.frame(x=1:10, y=rnorm(10), tms=Sys.time() + 1:10, id=gl(2, 5))
coordinates(d) <- ~x+y
## this avoids complaints later, but these are not real track data (!)
proj4string(d) <- CRS("+proj=laea +ellps=sphere")
(tr <- trip(d, c("tms", "id")))

## don't want adehabitatMA to be loaded as a requirement here
# }
# NOT RUN {
## a simple example with the common fixes required for basic track data

dat <- read.csv("trackfile.csv")
names(dat)  ## e.g. [1] "long" "lat" "seal" "date" "local" "lq"
library(sp)
coordinates(dat) <- c("long", "lat")

## date/times may be in a particular time zone, please check
dat$gmt <- as.POSIXct(strptime(paste(dat$date, dat$local),
                      "%d-%b-%y %H:%M:%S"), tz="GMT")

## if there are problems in the data, this will error
tr <- trip(dat, c("gmt", "seal"))

## the following code tries to fix common problems

## remove completely-duplicated rows
dat <- dat[!duplicated(dat), ]
## order the rows by seal, then by time
dat <- dat[order(dat$seal, dat$gmt), ]
## fudge duplicated times
dat$gmt <- adjust.duplicateTimes(dat$gmt, dat$seal)

## finally, convert to Spatial and create trip object
coordinates(dat) <- c("long", "lat")
tr <- trip(dat, c("gmt", "seal"))
# }
# NOT RUN {

# }
# NOT RUN {
   if (require(adehabitatLT)) {
     data(porpoise)
     porpoise <- as.trip(porpoise)
     proj4string(porpoise) <- CRS("+proj=utm +zone=21 +ellps=WGS84 +units=m +no_defs")
     summary(porpoise)

   }


   ## extended example to check that our projection metadata is correct
   library(maptools)
   data(wrld_simpl)
   library(rgeos)
   library(raster)

   ## 3 degrees either side (for half a zone . . .)
   ext <- as(extent(spTransform(porpoise, CRS(proj4string(wrld_simpl)))) + 3, "SpatialPolygons")
   proj4string(ext) <- CRS(proj4string(wrld_simpl))
   ## crop to the buffered tracks, and project to its native CRS
   w <- spTransform(gIntersection(wrld_simpl[grep("United States", wrld_simpl$NAME), ], ext),
    CRS(proj4string(porpoise)))

   plot(w)
   lines(porpoise)
# }

Run the code above in your browser using DataLab