If x
is your date vector, you can remove the empty levels as follows:
x = droplevels(x)
There may be a way to avoid creating an empty level, but at least it's an easy way to get rid of it.
Using your example:
droplevels(cut(as.Date("2013-12-31"), "year")) [1] 2013-01-01 Levels: 2013-01-01
Another option is to directly extract the year from the date object. For instance:
library(lubridate) year(as.Date(c("2013-12-31","2014-12-09","2014-11-10"))) [1] 2013 2014 2014
It will not be a factor, but you can always convert it to one if you want.
source share