Search code examples
rxtsforecastingisodateas.date

as.Date is throwing a row number mismatch, but all vectors are same length


The following (CSV) dataset has 3133 rows of expenses by day between 7/1/2000 and 12/31/2014:

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

The columns here are Year, Month, Day, Expense, and Count (for how many days of the each month had an expense).

I am trying to do a forecast out to the end of 2015, and need to deal with these messy date columns so I can slice and dice xts (?) objects with dplyr. ISOdate and as.Date functions are throwing this error:

> 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

What am I doing wrong? And should I instead build a vector of 5296 continuous dates between 7/1/2000 and 12/31/2014 and merge my 3133 rows of observations to this table (thus effectively inserting '0' in the Amount column for days on which there were no payments)?


Solution

  • Several errors (but not from paste): I'm guessing you were taught to use attach. That is probably the source of this particular error. Start by

    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
                                      )
                        )
    

    Then you can add further columns as needed.