Undesirable result when cutting by year in R

The cut.Date function has an undesirable function that, when applied by December 31, will add the 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 a lot of problems when processing the data later, I wonder if there is any alternative to reduce data by year without this strange function?

+6
source share
1 answer

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.

+14
source

Source: https://habr.com/ru/post/979417/


All Articles