firmo23
firmo23

Reputation: 8454

Convert ts matrix column back to class Date

Im trying to convert the date variable Trvl mon back into class Date (2019-01-01) with

andts[,1]<-as.Date(andts[,1],format="%Y-%m-%d")

dput(andts)
structure(c(17897, 17928, 17956, 17987, 18017, 18048, 18078, 
18109, 18140, 18170, 18201, 18231, 18262, 18293, 18322, 18353, 
18383, 18414, 18444, 18475, 18506, 18536, 18567, 18597, 18628, 
18659, 18687, 18718, 18748, 18779, 46405, 77984, 44869, 81882, 
76750, 86015, 73269, 4439, 1689, 52308, 37636, 55483, 22306, 
21552, 74636, 55701, 14099, 84315, 15568, 35756, 73793, 83296, 
47910, 97970, 57557, 67634, 20743, 97418, 8878, 82116), .Dim = c(30L, 
2L), .Dimnames = list(NULL, c("Trvl mon", "ctry cnt")), .Tsp = c(1, 
30, 1), class = c("mts", "ts", "matrix"))

Upvotes: 0

Views: 234

Answers (3)

G. Grothendieck
G. Grothendieck

Reputation: 270075

1) A ts object cannot mix Date and numeric; however, Date class is not needed here since the times actually represent year/months. Instead convert to a data frame and then to a zoo object with yearmon index class and from that we can directly convert to a ts series.

library(zoo)

DF <- as.data.frame(andts)
z <- read.zoo(DF, FUN = function(x) as.yearmon(as.Date(x)))
ts1 <- as.ts(z)

ts1

giving this ts series:

       Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov   Dec
2019 46405 77984 44869 81882 76750 86015 73269  4439  1689 52308 37636 55483
2020 22306 21552 74636 55701 14099 84315 15568 35756 73793 83296 47910 97970
2021 57557 67634 20743 97418  8878 82116         

2) This would also work if you are willing to hard code the start of the series. It gives t he same result as (1).

ts2 <- ts(andts[, -1], start = 2019, frequency = 12)

identical(ts1, ts2)
## [1] TRUE

3) If you really did want Date class then ts would not be appropriate but zoo, xts or data.frame would work.

library(zoo)
zz <- read.zoo(DF, FUN = as.Date)  # DF is from (1)

library(xts)
xx <- as.xts(zz)

fortify.zoo(zz) # data.frame

Upvotes: 1

henhesu
henhesu

Reputation: 851

You need to provide the origin:

library(magrittr)

andts <- structure(c(17897, 17928, 17956, 17987, 18017, 18048, 18078, 
                     18109, 18140, 18170, 18201, 18231, 18262, 18293, 18322, 18353, 
                     18383, 18414, 18444, 18475, 18506, 18536, 18567, 18597, 18628, 
                     18659, 18687, 18718, 18748, 18779, 46405, 77984, 44869, 81882, 
                     76750, 86015, 73269, 4439, 1689, 52308, 37636, 55483, 22306, 
                     21552, 74636, 55701, 14099, 84315, 15568, 35756, 73793, 83296, 
                     47910, 97970, 57557, 67634, 20743, 97418, 8878, 82116), .Dim = c(30L, 
                                                                                      2L), .Dimnames = list(NULL, c("Trvl mon", "ctry cnt")), .Tsp = c(1, 
                                                                                                                                                       30, 1), class = c("mts", "ts", "matrix"))

andts %>% 
  dplyr::as_tibble(andts) %>% 
  janitor::clean_names() %>% 
  dplyr::mutate(d = as.Date(trvl_mon, origin = '1970-01-01'))

#> # A tibble: 30 x 3
#>    trvl_mon ctry_cnt d         
#>       <dbl>    <dbl> <date>    
#>  1    17897    46405 2019-01-01
#>  2    17928    77984 2019-02-01
#>  3    17956    44869 2019-03-01
#>  4    17987    81882 2019-04-01
#>  5    18017    76750 2019-05-01
#>  6    18048    86015 2019-06-01
#>  7    18078    73269 2019-07-01
#>  8    18109     4439 2019-08-01
#>  9    18140     1689 2019-09-01
#> 10    18170    52308 2019-10-01
#> # … with 20 more rows

Created on 2021-07-05 by the reprex package (v2.0.0)

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 389235

If you want to keep the objects as time series you can use -

andts <- xts::xts(andts[, -1], as.Date(as.numeric(andts[,1]),origin = '1970-01-01'))

Or convert it to dataframe.

andts <- data.frame(andts)
andts[,1] <- as.Date(andts[,1],origin = '1970-01-01')

Upvotes: 1

Related Questions