Skip to content
Prev 44947 / 63424 Next

Suggestions for 'diff.default'

--- On Mon, 28/1/13, Suharto Anggono Suharto Anggono <suharto_anggono at yahoo.com> wrote:

            
Sorry, I wasn't careful enough. To obtain the correct class for the result, differencing should be done as many times as specified by argument 'differences'.

I consider the case of
diff(as.POSIXct(c("2012-01-01", "2012-02-01"), tz="UTC"), d=2)
versus
diff(diff(as.POSIXct(c("2012-01-01", "2012-02-01"), tz="UTC")))
To be safe, maybe just compute as usual, even when it is known that the end result will be empty. It can be done like this.

    empty <- integer()
    if (ismat)
	for (i in seq_len(differences))
	    r <- if (lag >= nrow(r))
                r[empty, , drop = FALSE] - r[empty, , drop = FALSE] else
                ...
    else
        for (i in seq_len(differences))
            r <- if (lag >= length(r))
                r[empty] - r[empty] else
                ...

If that way is used, 'xlen' is no longer needed.