42

I want to transform my ts object to data.frame object. My MWE is given below:

Code

set.seed(12345)
dat <- ts(data=runif(n=10, min=50, max=100), frequency = 4, start = c(1959, 2))
library(reshape2)
df <- data.frame(date=as.Date(index(dat)), Y = melt(dat)$value)

Output

         date        Y
1  1975-05-14 86.04519
2  1975-05-14 93.78866
3  1975-05-14 88.04912
4  1975-05-15 94.30623
5  1975-05-15 72.82405
6  1975-05-15 58.31859
7  1975-05-15 66.25477
8  1975-05-16 75.46122
9  1975-05-16 86.38526
10 1975-05-16 99.48685

I have lost my quarters in date columns. How can I figure out the problem?

6 Answers 6

51

How about

data.frame(Y=as.matrix(dat), date=time(dat))

This returns

          Y    date
1  86.04519 1959.25
2  93.78866 1959.50
3  88.04912 1959.75
4  94.30623 1960.00
5  72.82405 1960.25
6  58.31859 1960.50
7  66.25477 1960.75
8  75.46122 1961.00
9  86.38526 1961.25
10 99.48685 1961.50
3
  • 8
    (+1): Thanks @MrFlick as.Date(time(dat)) is more appropriate and did the trick.
    – MYaseen208
    Aug 17, 2014 at 20:07
  • 1
    The code as.Date(time(dat)) does not work for me. I am getting the error: Error in as.Date.default(time(dat)) : do not know how to convert 'time(dat)' to class “Date”.
    – djhurio
    Jan 4, 2019 at 8:46
  • 1
    @djhurio specifying zoo::as.Date helps
    – tjebo
    Apr 21, 2021 at 19:01
18

yearmon (from zoo) allows creating Date objects.

> dat <- ts(data=runif(n=10, min=50, max=100), frequency = 4, start = c(1959, 2))
> data.frame(Y=as.matrix(dat), date=as.Date(as.yearmon(time(dat))))
          Y       date
1  51.72677 1959-04-01
2  57.61867 1959-07-01
3  86.78425 1959-10-01
4  50.05683 1960-01-01
5  69.56017 1960-04-01
6  73.12473 1960-07-01
7  69.40720 1960-10-01
8  70.12426 1961-01-01
9  58.94818 1961-04-01
10 97.58294 1961-07-01
6
  • or data.frame(date = as.Date(as.yearmon(time(dat))), Y = coredata(dat)) Sep 18, 2015 at 12:50
  • 1
    Does not work for me. Had to use as.Date(paste(1, zoo::as.yearmon(time(dat))), format = "%d %b %Y").
    – djhurio
    Jan 4, 2019 at 8:43
  • Nowadays, I believe that broom makes it easy, you could simply broom::tidy(dat). Jan 4, 2019 at 9:01
  • @WilsonFreitas, nope. broom::tidy(dat) does not help. It just converts dat to tibble. No date conversation is done.
    – djhurio
    Jan 4, 2019 at 12:41
  • @djhurio, I think you changed the code in my comment introducing an error into the working code. The non-working code likely used zoo::as.yearmon rather than library(zoo) introducing the error which is due to as.Date.yearmon not being found. The code in my comment does work assuming the setup in the answer which in turn assumes library(zoo) was issued. Sep 6, 2022 at 12:29
10

The package timetk has several conversion functions. In your case:

dat <- ts(data=runif(n=10, min=50, max=100), frequency = 4, start = c(1959, 2))

timetk::tk_tbl(dat)

    # A tibble: 10 x 2
           index    value
   <S3: yearqtr>    <dbl>
 1       1959 Q2 86.04519
 2       1959 Q3 93.78866
 3       1959 Q4 88.04912
 4       1960 Q1 94.30623
 5       1960 Q2 72.82405
 6       1960 Q3 58.31859
 7       1960 Q4 66.25477
 8       1961 Q1 75.46122
 9       1961 Q2 86.38526
10       1961 Q3 99.48685
4

Seems that converting from xts objects seems to be both reliable and well documented. Below works and with the new date column in date / yearqtr class.

library(xts)
datx <- as.xts(dat)
df   <- data.frame(date=index(datx), coredata(datx))

Checking class of date:

class(df$date)
[1] "yearqtr"

And result:

print(df)

  date coredata.datx.
1  1959 Q2       86.04519
2  1959 Q3       93.78866
3  1959 Q4       88.04912
4  1960 Q1       94.30623
5  1960 Q2       72.82405
6  1960 Q3       58.31859
7  1960 Q4       66.25477
8  1961 Q1       75.46122
9  1961 Q2       86.38526
10 1961 Q3       99.48685
1

Package 'ggpp' provides function try_data_frame() (implemented using packages 'xts', 'zoo' and 'lubridate') that does the conversion in a single step. (This function is used in package 'ggpp' to implement a ggplot() method for time series, and returns the time index converted into a class that packages 'ggplot2' and 'scales' can use: Date or POSIXct.)

set.seed(12345)
dat.ts <- ts(data=runif(n=10, min=50, max=100), frequency = 4, start = c(1959, 2))

library(ggpp)
#> Loading required package: ggplot2
#> 
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#> 
#>     annotate
dat.df <- try_data_frame(dat.ts)

str(dat.df)
#> 'data.frame':    10 obs. of  2 variables:
#>  $ time  : Date, format: "1959-05-01" "1959-08-01" ...
#>  $ dat.ts: num  86 93.8 88 94.3 72.8 ...

dat.df
#>          time   dat.ts
#> 1  1959-05-01 86.04519
#> 2  1959-08-01 93.78866
#> 3  1959-11-01 88.04912
#> 4  1960-02-01 94.30623
#> 5  1960-05-01 72.82405
#> 6  1960-08-01 58.31859
#> 7  1960-11-01 66.25477
#> 8  1961-02-01 75.46122
#> 9  1961-05-01 86.38526
#> 10 1961-08-01 99.48685

Created on 2022-09-03 with reprex v2.0.2

See help(try_data_frame()) for the details on how to set the names of columns or alter the way in which dates or times are handled.

1

My function ts_as_tibble() can be used.

# Install `timeplyr` using the below code.
# remotes::install_github("NicChr/timeplyr")

library(timeplyr)

ts_as_tibble(dat)
#> # A tibble: 10 x 2
#>     time value
#>    <dbl> <dbl>
#>  1 1959.  86.0
#>  2 1960.  93.8
#>  3 1960.  88.0
#>  4 1960   94.3
#>  5 1960.  72.8
#>  6 1960.  58.3
#>  7 1961.  66.3
#>  8 1961   75.5
#>  9 1961.  86.4
#> 10 1962.  99.5

# Multiple series
ts_as_tibble(EuStockMarkets)
#> # A tibble: 7,440 x 3
#>    group  time value
#>    <chr> <dbl> <dbl>
#>  1 DAX   1991. 1629.
#>  2 DAX   1992. 1614.
#>  3 DAX   1992. 1607.
#>  4 DAX   1992. 1621.
#>  5 DAX   1992. 1618.
#>  6 DAX   1992. 1611.
#>  7 DAX   1992. 1631.
#>  8 DAX   1992. 1640.
#>  9 DAX   1992. 1635.
#> 10 DAX   1992. 1646.
#> # i 7,430 more rows

When the data contains multiple series, it is always converted to long format. Currently ts, mts, xts and timeSeries objects are supported.

The advantage of always returning it to long-format is that it can easily be passed to ggplot.

eu_stock <- ts_as_tibble(EuStockMarkets)
time_ggplot(eu_stock, time, value, group)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.