base-sort: Sort a 'timeSeries' by time stamps

sortR Documentation

Sort a 'timeSeries' by time stamps

Description

Sort a "timeSeries" object with respect to its time stamps.

Usage

## S3 method for class 'timeSeries'
sort(x, decreasing = FALSE, ...)

## S4 method for signature 'timeSeries'
is.unsorted(x, na.rm = FALSE, strictly = FALSE)

Arguments

x

a "timeSeries" object.

decreasing

a logical flag. Should we sort in increasing or decreasing order? By default FALSE.

na.rm

a logical value, should missing values be removed?

strictly

logical indicating if the check should be for strictly increasing values.

...

optional arguments passed to other methods.

Details

The method for sort sorts x either in increasing or decreasing time stamp order.

The method for is.unsorted returns TRUE if the time stamps of x are not sorted in increasing order (including the case when they are sorted in decreasing order) and FALSE otherwise. is.unsorted may also return NA when there are NAs among the time stamps of x.

All this is in line with the documented functionality of base::is.unsorted.

Value

for sort, a "timeSeries" object,

for the is.unsorted method, TRUE, FALSE, or NA, as described in section ‘Details’.

Note

If is.unsorted returns NA when there are NAs in the data but not in the time stamps use library{timeSeries} or call the function as timeSeries::is.unsorted. If you need more details, read the rest of this note.

base::is.unsorted 'sees' the method for "timeSeries" objects when package timeSeries is loaded (whether or not it is attached). However, due to the way base::is.unsorted is implemented, it may give wrong answers when there are NA's among the values of the time series. Developers of packages applying is.unsorted on timeSeries objects should import if from package timeSeries.

The above feature is not a shortcoming of base::is.unsorted but a consequence of the fact that the timeSeries method is not consistent with its semantics. For example, it works on the time stamps, while is.na works on the data values.

See Also

is.unsorted for further details on the NA case

Examples

   
## a monthly calendar series
x <- daily2monthly(LPP2005REC[, 1:2])[3:14, ]
   

## resample the series with respect to the time stamps,
resampled <- sample(x)
## the time stamps are unordered
resampled
is.unsorted(resampled) # TRUE (i.e., not sorted)
   
## Now sort the series in decreasing time order
backward_in_time <- sort(resampled, , decreasing = TRUE)
## time stamps ordered in decreasing order
## but is.unordered requires increasing order:
backward_in_time
is.unsorted(backward_in_time) # still TRUE
   
## Is the reverted series ordered?
forward_in_time <- rev(backward_in_time)
forward_in_time
is.unsorted(forward_in_time) # FALSE (i.e., sorted)

timeSeries documentation built on Sept. 30, 2024, 9:15 a.m.