Skip to contents

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

Usage

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

# S4 method for 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

Sorts a time series either in increasing or decreasing time stamp order. Internally the function order from R's base package is used. order generates a permutation which rearranges the time stamps in ascending or descending order.

To find out if the series is unsorted, use is.unsorted.

Value

for sort, a "timeSeries" object,

for the is.unsorted method, TRUE or FALSE

Examples

## 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)
   resampled
#> GMT
#>                     SBI          SPI
#> 2006-12-31  0.001144121 -0.001101976
#> 2006-10-31  0.002819157 -0.008238610
#> 2006-06-30 -0.000548353  0.014473814
#> 2006-05-31  0.000233945  0.009323326
#> 2006-04-30  0.001330360 -0.002943741
#> 2006-07-31  0.000077800  0.000267236
#> 2006-01-31 -0.000152952  0.002736095
#> 2006-09-30 -0.001218862  0.001409431
#> 2006-02-28  0.001071074 -0.011956984
#> 2006-08-31  0.000921518 -0.001466397
#> 2006-11-30  0.000453412 -0.008646808
#> 2006-03-31  0.000232369 -0.000929251
   is.unsorted(resampled)
#> [1] TRUE
   
## Now sort the serie in decreasing time order - 
   sorted <- sort(resampled, , decreasing = TRUE)
   sorted
#> GMT
#>                     SBI          SPI
#> 2006-12-31  0.001144121 -0.001101976
#> 2006-11-30  0.000453412 -0.008646808
#> 2006-10-31  0.002819157 -0.008238610
#> 2006-09-30 -0.001218862  0.001409431
#> 2006-08-31  0.000921518 -0.001466397
#> 2006-07-31  0.000077800  0.000267236
#> 2006-06-30 -0.000548353  0.014473814
#> 2006-05-31  0.000233945  0.009323326
#> 2006-04-30  0.001330360 -0.002943741
#> 2006-03-31  0.000232369 -0.000929251
#> 2006-02-28  0.001071074 -0.011956984
#> 2006-01-31 -0.000152952  0.002736095
   is.unsorted(sorted)
#> [1] TRUE
   
## Is the reverted series ordered? - 
   reverted <- rev(sorted)
   reverted
#> GMT
#>                     SBI          SPI
#> 2006-01-31 -0.000152952  0.002736095
#> 2006-02-28  0.001071074 -0.011956984
#> 2006-03-31  0.000232369 -0.000929251
#> 2006-04-30  0.001330360 -0.002943741
#> 2006-05-31  0.000233945  0.009323326
#> 2006-06-30 -0.000548353  0.014473814
#> 2006-07-31  0.000077800  0.000267236
#> 2006-08-31  0.000921518 -0.001466397
#> 2006-09-30 -0.001218862  0.001409431
#> 2006-10-31  0.002819157 -0.008238610
#> 2006-11-30  0.000453412 -0.008646808
#> 2006-12-31  0.001144121 -0.001101976
   is.unsorted(reverted)
#> [1] FALSE