There is an undesirable feature in function cut.Date
, which when applied on Dec 31 will add next year as a level:
cut(as.Date("2013-12-31"), "year")
[1] 2013-01-01
Levels: 2013-01-01 2014-01-01
This causes me a lot of troubles when processing data later, I wonder whether there is any alternative to cut data by year without this weird feature?
If x
is your date vector, you can remove empty levels as follows:
x = droplevels(x)
There may be a way to avoid the creation of an empty level in the first place, but at least this is a simple 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 example:
library(lubridate)
year(as.Date(c("2013-12-31","2014-12-09","2014-11-10")))
[1] 2013 2014 2014
This won't be a factor, but you can always convert it to one if you'd like.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With