Quantcast
Viewing all articles
Browse latest Browse all 206278

How to extract quarters from dates?

Why doesn't the cut at "3 months" produce labels as expected?

 # create time series data
 everyday <- seq(from = as.Date('2014-1-1'), to = as.Date('2014-12-31'), by = 'day')

 # create a factor based on the quarter of the year an observation is in:
 qtrs <- cut(everyday, "3 months", labels = paste0('Q', 1:4))

 ## Error in cut.default(unclass(x), unclass(breaks), labels = labels, 
 ## right = right,  : 
 ##   lengths of 'breaks' and 'labels' differ

The cut is every 3 months, so that would create 4 Quarters and I'd expect to need 4 labels, but the error message suggests that the length of breaks and labels differs.

 qtrs <- cut(everyday, "3 months", labels = paste0('Q', 1:5))
 table(qtrs)
 ## qtrs
 ## Q1 Q2 Q3 Q4 Q5 
 ## 90 91 92 92  0 

The fifth label Q5 seems to be needed and yet appears with a zero count.

The example is taken from "Data Manipulation with R" by Phil Spector,

http://www.springer.com/statistics/computational+statistics/book/978-0-387-74730-9


Viewing all articles
Browse latest Browse all 206278

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>