0

last does not work on vectors of dates:

library(data.table)
f <- c("2014-11-10","2014-11-11")
last(f)
[1] "2014-11-11"
last(as.Date(f))
Error in last(as.Date(f)) : 
  data.table::last is trying to defer to xts::last because either x is not a vector, list, data.frame or data.table, or parameters such as 'n' or 'keep' have been provided as well. But xts hasn't been loaded.

the "helpful" message promised by ?last says that the argument is not a vector (it is!) and requires a package xts which I don't know about.

what is going on?

sds
  • 58,617
  • 29
  • 161
  • 278
  • `as.Date(last(f))` ? – rawr Nov 10 '14 at 14:35
  • @konvas: turn your comment into an answer and I will accept it, see, however, [How come as.Date affects is.vector?](http://stackoverflow.com/questions/26847171/how-come-as-date-affects-is-vector) – sds Nov 10 '14 at 15:30

1 Answers1

0

Pasting from comments above, is.vector(as.Date(f)) is FALSE, so it is not a vector. You need to first load xts using library(xts) and then load data.table and your code should work (since then data.table will be able to call xts::last on the Date object).

konvas
  • 14,126
  • 2
  • 40
  • 46