Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undesirable result when cutting by year in R

Tags:

r

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?

like image 670
user3684014 Avatar asked Dec 10 '14 20:12

user3684014


1 Answers

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.

like image 55
eipi10 Avatar answered Oct 15 '22 11:10

eipi10