1

I have this data containing dates from 2000 to 2020:

date[1:5]
# [1] "2000-01-01" "2000-01-02" "2000-01-03" "2000-01-04" "2000-01-05" 

str(date)
# Date[1:941], format: 2000-01-01" "2000-01-02" "2000-01-03" "2000-01-04"..

I can do this:

indices <- cut(date, "year")

Then all dates from "2000-01-01" to "2000-12-31" will be mapped to "2000-01-01".

But I want all dates from "1999-12-01" to "2000-11-30" to be mapped to "2000-01-01".

I want the equivalent of cut(date, "year"), but where the year is shifted to start on a month other than January.

How can I do this?

shadowtalker
  • 12,529
  • 3
  • 53
  • 96
temor
  • 935
  • 2
  • 10
  • 26

1 Answers1

1

One way of solving this problem is defining a sequence of desired breaks and the associated labels. Such as this:

date<-as.Date(c("2000-01-01", "2000-01-02", "2000-01-03", "2000-01-04", "2000-01-05"))

#define break points
cutpoints<-seq.Date(as.Date("1999-12-01"), by="1 year", length.out = 23)
#define labels (1 less than the number of breaks)
names<-seq.Date(as.Date("2000-01-01"), by="1 year", length.out = 22)


cut(date, breaks=cutpoints, labels = names)
Dave2e
  • 22,192
  • 18
  • 42
  • 50