【问题标题】:Time series to data frame时间序列到数据框
【发布时间】:2019-06-30 03:09:05
【问题描述】:

AIM:将时间序列对象转换为数据框。

数据: datasets::Seatbelts

问题:这是一个时间序列对象,具有一个月和一年的时间戳。我想在单独的列中提取月份和年份。见:

> Seatbelts
         DriversKilled drivers front rear   kms PetrolPrice VanKilled law
Jan 1969           107    1687   867  269  9059     0.10297        12   0
Feb 1969            97    1508   825  265  7685     0.10236         6   0
Mar 1969           102    1507   806  319  9963     0.10206        12   0
Apr 1969            87    1385   814  407 10955     0.10087         8   0
May 1969           119    1632   991  454 11823     0.10102        10   0
Jun 1969           106    1511   945  427 12391     0.10058        13   0
Jul 1969           110    1559  1004  522 13460     0.10377        11   0

我已经看过这篇文章了:

Converting ts object to data.frame

该解决方案的问题:

data.frame(as.matrix(seatbelts), date=time(seatbelts))

是不是我得到一个只有年份的列日期,月份似乎丢失了:

> data.frame(as.matrix(seatbelts), date=time(seatbelts))
    DriversKilled drivers front rear   kms PetrolPrice VanKilled law date
1             107    1687   867  269  9059     0.10297        12   0 1969
2              97    1508   825  265  7685     0.10236         6   0 1969
3             102    1507   806  319  9963     0.10206        12   0 1969
4              87    1385   814  407 10955     0.10087         8   0 1969
5             119    1632   991  454 11823     0.10102        10   0 1969
6             106    1511   945  427 12391     0.10058        13   0 1969

我想要一个包含月份和年份的日期列,或者一个包含日期和年份的单独列。

【问题讨论】:

    标签: r time-series


    【解决方案1】:

    你得到的日期实际上是一个小数年,所以月份仍然在那里。如果你想有两列,年和月,你可以试试这样的:

    res <- data.frame(as.matrix(Seatbelts), date=time(Seatbelts))
    res$year <- trunc(res$date)
    res$month <- (res$date - res$year) * 12 + 1
    res
    
    ##  DriversKilled drivers front rear   kms PetrolPrice VanKilled law     date year month
    ## 1           107    1687   867  269  9059   0.1029718        12   0 1969.000 1969     1
    ## 2            97    1508   825  265  7685   0.1023630         6   0 1969.083 1969     2
    ## 3           102    1507   806  319  9963   0.1020625        12   0 1969.167 1969     3
    ## 4            87    1385   814  407 10955   0.1008733         8   0 1969.250 1969     4
    ## 5           119    1632   991  454 11823   0.1010197        10   0 1969.333 1969     5
    ## 6           106    1511   945  427 12391   0.1005812        13   0 1969.417 1969     6
    

    【讨论】:

      【解决方案2】:

      我可能误解了这个问题,但要获得月份和年份以及月份年份日期的输出,您可以先转换为 xts 格式,然后再转换为数据框 - 后一种转换通常比 @ 更好地记录987654322@ 对象。以下操作相当初级 - 将文本提取到新列中。

      library(xts)
      library(tidyverse)
      
      tsx <- as.xts(datasets::Seatbelts)
      df <- data.frame(date=index(tsx), coredata(tsx)) %>%
        mutate(date_chr = as.character(date),
               month    = substr(date, 1, 3),
               year     = substr(date, 5, 8))
      
            date DriversKilled drivers front rear   kms PetrolPrice VanKilled law date_chr month year
      1 Jan 1969           107    1687   867  269  9059   0.1029718        12   0 Jan 1969   Jan 1969
      2 Feb 1969            97    1508   825  265  7685   0.1023630         6   0 Feb 1969   Feb 1969
      3 Mar 1969           102    1507   806  319  9963   0.1020625        12   0 Mar 1969   Mar 1969
      4 Apr 1969            87    1385   814  407 10955   0.1008733         8   0 Apr 1969   Apr 1969
      5 May 1969           119    1632   991  454 11823   0.1010197        10   0 May 1969   May 1969
      6 Jun 1969           106    1511   945  427 12391   0.1005812        13   0 Jun 1969   Jun 1969
      

      【讨论】:

        猜你喜欢
        • 2013-12-25
        • 2018-08-13
        • 1970-01-01
        • 1970-01-01
        • 2015-07-28
        • 2023-01-26
        • 2018-10-29
        • 2016-04-05
        • 2014-06-19
        相关资源
        最近更新 更多