Learn R Programming

timeSeries (version 4041.111)

sort: Sort a 'timeSeries' by time stamps

Description

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

Usage

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

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

Value

for sort, a "timeSeries" object,

for the is.unsorted method, TRUE, FALSE, or

NA, as described in section ‘Details’.

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.

See Also

is.unsorted for further details on the NA case

Examples

Run this code
## a monthly calendar series
x <- daily2monthly(LPP2005REC[, 1:2])[3:14, ]
   
set.seed(1234)
## 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)

Run the code above in your browser using DataLab