【问题标题】:creation of a time series adds day and time to the date创建时间序列将日期和时间添加到日期
【发布时间】:2017-11-17 01:38:54
【问题描述】:

我的数据框看起来像这样(有更多的列和行):

DT 
           Date PERMNO lag.ME.Jun
  <S3: yearmon> <fctr>      <dbl>
1      Gen 2000  34936     21.860
2      Feb 2000  34936     21.860
3      Mar 2000  34936     21.860

然后我为每一列创建不同的时间序列(例如,对于变量 lag.ME.Jun):

v6<-xts( newdata11$lag.ME.Jun, newdata11$Date)

但是,它还在时间序列中添加了日期和时间;没有提供。所以 v6 看起来像:

                    34936
2000-01-01 01:00:00 21.86
2000-02-01 01:00:00 21.86
2000-03-01 01:00:00 21.86 

如何避免时间序列中出现日期和时间?只有月份和年份。

【问题讨论】:

标签: r time format time-series xts


【解决方案1】:

看来newdata11$Date 要么是POSIXct 类型,要么被转换为一个。如果yearmon 是您想要的,请明确说明:

v6<-xts( newdata11$lag.ME.Jun, as.yearmon(newdata11$Date))

例如

d = as.yearmon(c("mar07", "apr07", "may07"), "%b%y")
> d
[1] "Mar 2007" "Apr 2007" "May 2007"
> xts(1:3, d)
         [,1]
Mar 2007    1
Apr 2007    2
May 2007    3
> d2= as.Date(c("2017-05-01", "2017-06-01", "2017-07-01"))
> xts(1:3, as.yearmon(d2))
         [,1]
May 2017    1
Jun 2017    2
Jul 2017    3

【讨论】: