R/setNA.r

Defines functions setNA

Documented in setNA

setNA <- function(ltraj, date.ref, dt, tol=dt/10,
                  units=c("sec", "min", "hour", "day"), ...)
{
    if (!inherits(ltraj,"ltraj"))
        stop("ltraj should be of class \"ltraj\"")
    if (!attr(ltraj, "typeII"))
        stop("ltraj should be of type II (time recorded)")
    if (is.numeric(date.ref)) {
        class(date.ref) <- c("POSIXct","POSIXt")
    }
    if (inherits(date.ref,"POSIXlt"))
        date.ref <- as.POSIXct(date.ref)
    if (!inherits(date.ref,"POSIXct"))
        stop("date.ref should be of class \"POSIXct\"")

    units <- match.arg(units)
    dt <- .convtime(dt, units)
    tol <- .convtime(tol, units)
    if (length(date.ref)==1)
        date.ref <- rep(date.ref, length(ltraj))

    res <- lapply(1:length(ltraj), function(oo) {
         x <- ltraj[[oo]]
         date.refp <- date.ref[oo]
         dc <- x$date
         da <- as.numeric(x$da) - as.numeric(date.refp)
         glou <- round(da/dt,0)*dt + as.numeric(date.refp)
         if (any(abs(as.numeric(dc) - as.numeric(glou)) > tol))
            stop("ltraj contains irregular data (time lag > or < tol)")
         laou <- as.integer(round(da/dt,0))
         mlaou <- min(laou)
         laou <- laou-mlaou+1
         xx <- rep(NA, max(laou))
         yy <- rep(NA, max(laou))
         da <- (((1:max(laou))-1)+mlaou)*dt + as.numeric(date.refp)
         xx[laou] <- x$x
         yy[laou] <- x$y
         da[laou] <- x$date
         class(da) <- c("POSIXct","POSIXt")
         return(as.ltraj(data.frame(xx,yy), da, id=attr(x,"id"),
                         burst = attr(x,"burst"), typeII=TRUE,...))
     })
    return(do.call("c.ltraj",res))
}

Try the adehabitat package in your browser

Any scripts or data that you put into this service are public.

adehabitat documentation built on Jan. 28, 2018, 5:02 p.m.