I'm trying to do a regular reverse sort, but I don't understand how to do it with an xts
object. I want the last date to be in the first line and not the last.
I tried several ways but it doesn't work.
library(xts)
x <- rnorm(500) |> cumsum() |> round(1) |>
xts(Sys.time()+1:500) |> to.minutes(name = NULL)
# dont work :(
# x[nrow(x):1,]
# x[order(index(x),decreasing = T),]
# rev(x)
I think you can't.
A possible work-around might be:
xr = as.matrix(x)[rev(seq_len(nrow(x))),,drop=FALSE]
xr
#> Open High Low Close
#> 2024-07-04 16:22:06.941923 -8.8 -5.9 -8.8 -6.6
#> 2024-07-04 16:21:59.941923 -8.0 -3.4 -12.6 -7.8
#> 2024-07-04 16:20:59.941923 -1.7 1.1 -7.7 -7.7
#> 2024-07-04 16:19:59.941923 -4.4 4.9 -5.1 -3.0
#> 2024-07-04 16:18:59.941923 0.1 2.4 -6.5 -5.7
#> 2024-07-04 16:17:59.941923 -1.1 1.4 -9.7 -1.0
#> 2024-07-04 16:16:59.941923 9.9 14.2 -5.4 -1.5
#> 2024-07-04 16:15:59.941923 -2.4 10.8 -4.2 9.5
#> 2024-07-04 16:14:59.941923 -0.6 0.9 -10.4 -2.8
#> 2024-07-04 16:13:59.941923 0.6 1.0 -2.1 -2.1
class(xr)
#> [1] "matrix" "array"
suppressMessages(library(xts))
x = rnorm(500) |> cumsum() |> round(1) |>
xts(Sys.time()+1:500) |> to.minutes(name = NULL)