簡體   English   中英

將 ts object 轉換為 data.frame

[英]Converting ts object to data.frame

我想將我的ts object 轉換為data.frame object。 我的 MWE 如下所示:

代碼

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

我在日期列中丟失了宿舍。 我怎樣才能找出問題所在?

怎么樣

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

這返回

          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

yearmon (來自zoo )允許創建Date對象。

> 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

包 timetk 有幾個轉換功能。 在你的情況下:

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

似乎從xts對象轉換似乎既可靠又有據可查。 下面的作品和 date / yearqtr 類中的新日期列。

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

檢查date類別:

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

結果:

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

Package 'ggpp' 提供了 function try_data_frame() (使用包 'xts'、'zoo' 和 'lubridate' 實現),只需一步即可完成轉換。 (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

使用reprex v2.0.2創建於 2022-09-03

有關如何設置列名或更改處理日期或時間的方式的詳細信息,請參閱help(try_data_frame())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM