0

I'm currently busy building an R Script to format some data for later use. I wanted to bin the date into quarters and wrote the following function to do so.

dates_to_quarters <- function(x){
    x <- as.numeric(month(x))

    x[x > 0 & x < 4 & is.na(x) == FALSE] <- "Q1"
    x[x >= 4  & x < 7 & is.na(x) == FALSE] <- "Q2"
    x[x >= 7 & x < 10 & is.na(x) == FALSE] <- "Q3"
    x[x >= 10 & x < 13 & is.na(x) == FALSE] <- "Q4"

    return(x)
}

I checked for NA Values because im new to R and don't know how they behave exactly in comparisons. Notice that x is a vector of Dates that have been formatted using the anytime() function.

After using the above function on a Date Vector, almost all records have the correct class. Except for those that should belong to the third quarter. They kept the number of the month as a value.

When I move the first two statements for Q1 and Q2 below the others, the code works as it should.

I started to play around with the console and noticed, that after executing only the statement for Q1, using the boolean selection for Q3 no longer yields a result. And after that I noticed that "Q1" > 7 yields TRUE.

Does anybody know whats going on? (Im new the R as you might have guessed)

EDIT:

Example Data (this is passed to the function)

20-01-2017
20-04-2017
20-09-2017
20-12-2017

Expected output:

Q1
Q2
Q3
Q4

Thats what I get:

Q1
Q2
9
Q4

1 Answers1

1

1) yearqtr Convert the character dates to "yearqtr" class and then format them using the %q format specifier:

x <- c("20-01-2017", "20-04-2017", "20-09-2017", "20-12-2017") # input

library(zoo)

format(as.yearqtr(x, "%d-%m-%Y"), "Q%q")
## [1] "Q1" "Q2" "Q3" "Q4"

2) quarters This uses no packages:

quarters(as.Date(x, "%d-%m-%Y"))
## [1] "Q1" "Q2" "Q3" "Q4"

Update: Modified input x to correspond to changes in question.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341