0

I have a Time Series object, but I would like to transpose it in such a way that I will obtain a matrix with one column without losing the TS type.

Something like:

Time-Series[1:492 , 1] from 1976 to 2017

Every time I try to manipulate the time series, it will lose the TS "type", becoming num.

How can I solve this?

rmlockerd
  • 3,776
  • 2
  • 15
  • 25
Jrghen
  • 11
  • 2
  • could you [use `dput`](https://stackoverflow.com/questions/49994249/example-of-using-dput) to provide a sample of the time serie? – Waldi Oct 06 '20 at 16:16

1 Answers1

0

1) dim Assuming that you have a ts series such as the one shown below try setting its dimensions using dim<- :

tt <- ts(1:10, start = 2000, freq = 4)
dim(tt) <- c(length(tt), 1)

dim(tt)
## [1] 10  1

class(tt)
## [1] "ts"

1a) This can alternately be written as:

tt <- ts(1:10, start = 2000, freq = 4)
tt2 <- `dim<-`(tt, c(length(tt), 1))

dim(tt2)
## [1] 10  1

class(tt2)
## [1] "ts"

frequency(tt2)
## [1] 4

start(tt2)
## [1] 2000    1

2) cbind An alternative is to use cbind. Note that it may not display as 10x1 but it is internally which can be checked using dim(...).

cbind(tt, tt)[, 1, drop = FALSE]

3) zoo Unfortunately cbind(tt) does not give a 10x1 result as one might expect but we can convert to zoo, use cbind and convert back. Again it does not display as 10x1 but internally it is 10x1 and can be checked using dim(...).

library(zoo)
as.ts(cbind(as.zoo(tt)))

Most manipulations using zoo are more straight forward than with ts so you could alternately just use zoo objects.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341