as.Date抛出行号不匹配,但所有向量都是相同的长度

时间:2015-02-17 20:34:11

标签: r xts forecasting isodate as.date

以下(CSV)数据集在7/1/2000和2014年12月31日之间每天有3133行费用:

head(d_exp_0014)
2000    7   6   792078.595  9
2000    7   7   140065.5    9
2000    7   11  190553.2    9
2000    7   12  119208.65   9
2000    7   16  1068156.293 9
2000    7   17  0           9
2000    7   21  457828.8033 9
2000    7   26  661445.0775 9
2000    7   28  211122.82   9
2000    8   2   273575.1733 8

这里的列是年,月,日,费用和计数(每个月有多少天有费用)。

我正在尝试预测到2015年底,并且需要处理这些凌乱的日期列,以便我可以使用dplyr对xts(?)对象进行切片和切块。 ISOdate和as.Date函数抛出了这个错误:

> exp <- data.frame(data = d_exp_0014, Date = as.Date(paste(Year, Month, Day), format = "m%/d%/Y%"), Amount = Amount, Count = Count, t = c(1:3133))
Error in data.frame(data = d_exp_0014, Date = as.Date(paste(Year, Month,  : 
  arguments imply differing number of rows: 3133, 3134
> length(d_exp_0014$Year)
[1] 3133
> length(d_exp_0014$Month)
[1] 3133
> length(d_exp_0014$Day)
[1] 3133

我做错了什么?而且我应该在7/1/2000和2014年12月31日之间构建一个5296个连续日期的向量,并将我的3133行观察值合并到此表中(因此有效地在Amount列中插入'0'没有付款)?

1 个答案:

答案 0 :(得分:1)

有几个错误(但不是来自paste):我猜你被教导要使用attach。这可能是这个特殊错误的根源。从

开始
detach(d_exp_0014)
d_exp_0014 <- cbind(d_exp_0014, 
                    myDate = with(d_exp_0014,
                                as.Date(paste(Year, Month, Day, sep="/"), 
                                     format = "%Y/%m/%d") # note % first then letter
                                  )
                    )

然后您可以根据需要添加更多列。