简体   繁体   中英

data.table:last fails on date vectors

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM