0

how can I transpose a xts Matrix?

I do this before

M<-Z
d <- as.Date(1:nrow(M))
M<- xts(M, order.by=d)
class(M)
[1] "xts" "zoo"

# Matrix M has size 700x70 as xts format. Everything ok!  

But I need these Matrix transponed as 70 x 700 format.

How can I transpose without to loose the xts format?

TM<- t(M)
class(TM)
[1] "matrix" "array"

I tried this:

as.xts(TM)
Error in as.POSIXlt.character(x, tz, ...) : 
character string is not in a standard unambiguous format.

The 700 columns has the date, but only the xts isn´t more avalible.

Thank you for any ideas and helping.

Best regards.

  • If you transpose the xts object, what will happen to the index of `M`? It is now a vector of column names and the xts structure (class, its index) is gone. You will always need to index the transpose. – Rui Barradas Jul 09 '22 at 22:06
  • @Rui Barradas Thank you for reply. Now I tried these way, but it comes an other error. ```M<-Z TM<- t(M) d <- as.Date(1:ncol(TM)) N<- xts(TM, order.by=d) Error in xts(TM, order.by = d) : NROW(x) must match length(order.by)``` I´m not sure what can I do. Unfortunately I dont know what should I do with the index. Thank you for an idea. – Normalverteilt Jul 09 '22 at 22:41
  • 2
    The transpose of an xts object CANNOT be an xts object. The rows of the original were ordered but the columns were not. XTS objects are a variant of zoo object and the rows need to be ordered. So the class of xts objects is not closed under the operation of transposition. I'm thinking this is an X-Y problem. You are asking us to tell you how to do X so you can do Y but you are not telling us what Y really is. – IRTFM Jul 10 '22 at 01:09
  • If you share a sample of Z and the libraries you are using, you're more likely to get an answer. The code and error you posted are great, but the contents of Z are crucial to help you. The code is not reproducible without Z.. – guasi Jul 10 '22 at 01:58
  • It should be `d <- as.Date(1:nrow(TM))`, not `ncol`. That's what the error message is saying. – Rui Barradas Jul 10 '22 at 03:20
  • @IRTFM thank you for describe the problem. Yes, maybe it exist an other way. I would like use the PANICr package ```getnfac(x,kmax,criteria)```. It need the transposed matrix as xts format. The matrix must be in xts, with rows as time series. My matrix has 70 columns as time series. But if it not possible to transponse an xts matrix, than nobody could use these package. Thank you for any idea. – Normalverteilt Jul 10 '22 at 10:22
  • 2
    Why do you need to transpose an xts matrix? The function `getnfac` works when you give an xts objects as an input. `data(sample_matrix) ; sample.xts <- as.xts(sample_matrix) ; getnfac(sample.xts, kmax = 2)` works without an issue. – phiver Jul 10 '22 at 13:31
  • ?getnfac says that the first argument should be a matrix. It does not require an xts object. – G. Grothendieck Jul 18 '22 at 14:17

0 Answers0