There has to be a better way

14. July 2016 data, programming, r 1
There has to be a better way

On my journey to becoming an R data ninja, I’ve learned a lot and have come a long way.  However, today, I found myself writing a ginormous nested ifelse statement in R.  I needed to code the quarter (Q1, Q2, Q3, and Q4) for over 6,000 rows and standard packages weren’t cutting it.  The Lubridate package has an awesome quarter() function that automatically calculates the quarter for a date, but it is based on the calendar year.  The first quarter in the calendar year starts on January 1, and the data I’m working with is based on the fiscal year starting on December 1.

statement: quarter(mdy(01-01-2016))
result: Q1

statement: quarter(mdy(12-01-2015))
result: Q4

While writing the if else statement, I kept reading article after article and package after package to find a more efficient way to accomplish this task. One glimmer of hope that I ran across was housed in the github repository for the Lubridate package.  I found a pull request that has been submitted.  The submitter, multiphrenic coded an extra parameter in the function that will allow users to shift the start month.  If this gets accepted, I’ll be one happy camper!

I came up empty handed and went on finishing the statement. Although I was able to get everything coded correctly, I’m still on the hunt for a better way to finish this task.

Below is an example of my giant ifelse statement/mess.  It runs, it’s just not pretty and I think it’s fairly inefficient.

x <- "DEC"

x <- ifelse(x == "DEC", "Q1",
       ifelse(x == "JAN", "Q1",
         ifelse(x == "FEB", "Q1",
           ifelse(x == "MAR", "Q2",
             ifelse(x == "APR", "Q2",
               ifelse(x == "MAY", "Q2",
                 ifelse(x == "JUN", "Q3",
                   ifelse(x == "JUL", "Q3",
                     ifelse(x == "AUG", "Q3",
                       ifelse(x == "SEP", "Q4",
                         ifelse(x == "OCT", "Q4",
                           ifelse(x == "NOV", "Q4", "ERROR")
                                )
                              )
                            )
                          )
                        )
                      )
                    )
                  )
                )
             ))

result: Q1

1 thought on “There has to be a better way”

Leave a Reply