I was wondering how can I convert a time series into a data frame, with columns intact. Attached is the Time Series which I am working with.
structure(c(74, 179, 464, 880, 324, 184, 90, 170, 140, 96, 78,
83, 83, 121, 245, 740, 332, 123, 117, 138, 20, 42, 70, 70, 42,
103, 490, 641, 488, 245, 142, 95, 63, 343, 57, 113, 100, 105), tsp = c(2019.66666666667,
2022.75, 12), class = "ts")
The Time output is : Time(Temp):
Jan Feb Mar Apr May Jun Jul Aug Sep Oct
2019 2019.667 2019.750
2020 2020.000 2020.083 2020.167 2020.250 2020.333 2020.417 2020.500 2020.583 2020.667 2020.750
2021 2021.000 2021.083 2021.167 2021.250 2021.333 2021.417 2021.500 2021.583 2021.667 2021.750
2022 2022.000 2022.083 2022.167 2022.250 2022.333 2022.417 2022.500 2022.583 2022.667 2022.750
Nov Dec
2019 2019.833 2019.917
2020 2020.833 2020.917
2021 2021.833 2021.917
2022
Can someone tell me if its possible to convert this into a dataframe such that there are two columns, one column with the date and the other date with the values.
I tried this but this doesn't get the time series in a column this just converts the values.
Temp <- as.data.frame(tsclean(Temp))
CodePudding user response:
Hope this can help you:
library(forecast)
#> Registered S3 method overwritten by 'quantmod':
#> method from
#> as.zoo.data.frame zoo
Temp = structure(c(74, 179, 464, 880, 324, 184, 90, 170, 140, 96, 78,
83, 83, 121, 245, 740, 332, 123, 117, 138, 20, 42, 70, 70, 42,
103, 490, 641, 488, 245, 142, 95, 63, 343, 57, 113, 100, 105), tsp = c(2019.66666666667,
2022.75, 12), class = "ts")
data.frame(time = time(Temp),
year = as.integer(time(Temp)),
month = cycle(Temp),
date = as.Date(sprintf("%d-%d-01", as.integer(time(Temp)), cycle(Temp))),
Temp= tsclean(Temp))
#> time year month date Temp
#> 1 2019.667 2019 9 2019-09-01 74.0000
#> 2 2019.750 2019 10 2019-10-01 179.0000
#> 3 2019.833 2019 11 2019-11-01 464.0000
#> 4 2019.917 2019 12 2019-12-01 664.6451
#> 5 2020.000 2020 1 2020-01-01 324.0000
#> 6 2020.083 2020 2 2020-02-01 184.0000
#> 7 2020.167 2020 3 2020-03-01 90.0000
#> 8 2020.250 2020 4 2020-04-01 170.0000
#> 9 2020.333 2020 5 2020-05-01 140.0000
#> 10 2020.417 2020 6 2020-06-01 96.0000
#> 11 2020.500 2020 7 2020-07-01 78.0000
#> 12 2020.583 2020 8 2020-08-01 83.0000
#> 13 2020.667 2020 9 2020-09-01 83.0000
#> 14 2020.750 2020 10 2020-10-01 121.0000
#> 15 2020.833 2020 11 2020-11-01 519.2210
#> 16 2020.917 2020 12 2020-12-01 740.0000
#> 17 2021.000 2021 1 2021-01-01 332.0000
#> 18 2021.083 2021 2 2021-02-01 123.0000
#> 19 2021.167 2021 3 2021-03-01 117.0000
#> 20 2021.250 2021 4 2021-04-01 138.0000
#> 21 2021.333 2021 5 2021-05-01 20.0000
#> 22 2021.417 2021 6 2021-06-01 42.0000
#> 23 2021.500 2021 7 2021-07-01 70.0000
#> 24 2021.583 2021 8 2021-08-01 70.0000
#> 25 2021.667 2021 9 2021-09-01 42.0000
#> 26 2021.750 2021 10 2021-10-01 103.0000
#> 27 2021.833 2021 11 2021-11-01 490.0000
#> 28 2021.917 2021 12 2021-12-01 641.0000
#> 29 2022.000 2022 1 2022-01-01 358.8305
#> 30 2022.083 2022 2 2022-02-01 245.0000
#> 31 2022.167 2022 3 2022-03-01 142.0000
#> 32 2022.250 2022 4 2022-04-01 95.0000
#> 33 2022.333 2022 5 2022-05-01 63.0000
#> 34 2022.417 2022 6 2022-06-01 62.2313
#> 35 2022.500 2022 7 2022-07-01 57.0000
#> 36 2022.583 2022 8 2022-08-01 113.0000
#> 37 2022.667 2022 9 2022-09-01 100.0000
#> 38 2022.750 2022 10 2022-10-01 105.0000