Skip to content
Home » Counting, adding or subtracting business days in R

Counting, adding or subtracting business days in R

Tags:

Calculating the number of days between two dates in R is as simple as using + or -. However, if you only want to count the business days (e.g. in a B2B context), you’ll have to be a little more creative. Let’s just do that.

Count business days in R

There are some very creative ways to count business days, but the most convenient one is definitely by using the bizdays R package.

Calculating the number of business days can be done using the (identically named) bizdays function. It takes only three arguments that you’ll need to achieve your goals: (1) the start date, (2) the end data and (3) a calendar object.

A calendar object can created using the create.calendar function. In this object you pass arguments that define which days are business days and which aren’t:

  • weekdays: A vector of characters that define which days of the week are not business days
  • holidays: A vector of Dates that define which dates are not business days

By using the create.calendar function and the bizdays function, it’s fairly easy to count business days between two dates.

library(bizdays)
business_calendar <- create.calendar('my_calendar', weekdays = c('saturday','sunday'))

# data.table way
df[,n_days := bizdays(start_date, end_date, cal = business_calendar)]

# dplyr way
df %>% 
	mutate(n_days = bizdays(start_date, end_date, cal = business_calendar))

Add or subtract business days in R

In the next example, I do not want to count the number of days between two dates. I want to add or subtract some business days from a specific date. Using the bizdays library, that’s once again very easy. It contains an offset() function to add or subtract business days from a specific date. Here, too, you need to specify the calendar object you’d like to use.

A good tip is to specify the library with the function, because offset is a fairly used function name in other libraries.

library(bizdays)
business_calendar <- create.calendar('my_calendar', weekdays = c('saturday','sunday'))

# data.table way
df[,added_10_biz_days := bizdays::offset(my_date, 10, cal = business_calendar)] # add
df[,added_10_biz_days := bizdays::offset(my_date, -10, cal = business_calendar)] # subtract

# dplyr way
df %>% 
	mutate(added_10_biz_days = bizdays::offset(my_date, 10, cal = business_calendar)) # add
	mutate(added_10_biz_days = bizdays::offset(my_date, 10, cal = business_calendar)) # subtract

Great success!

Say thanks, ask questions or give feedback

Technologies get updated, syntax changes and honestly… I make mistakes too. If something is incorrect, incomplete or doesn’t work, let me know in the comments below and help thousands of visitors.

10 thoughts on “Counting, adding or subtracting business days in R”

  1. I am currently writing a paper and a bug appeared in the paper. I found what I wanted from your article. Thank you very much. Your article gave me a lot of inspiration. But hope you can explain your point in more detail because I have some questions, thank you. 20bet

  2. And the elements of the political process will be turned into a laughing stock, although their very existence brings undoubted benefit to society. The ideological considerations of the highest order, as well as the constant quantitative growth and the scope of our activity ensures the relevance of the positions occupied by participants in relation to the tasks.

  3. In their desire to improve the quality of life, they forget that a high -quality prototype of the future project plays a decisive importance for the personnel training system corresponding to pressing needs. As well as the diagrams of relations, which are a vivid example of the continental-European type of political culture, will be devoted to a socio-democratic anathema.

  4. We are forced to build on the fact that the economic agenda of today speaks of the possibilities of priority requirements. Being just part of the overall picture, many famous personalities are ambiguous and will be subjected to a whole series of independent research!

  5. Just as the basic development vector leaves no chance for the mass participation system. The ideological considerations of the highest order, as well as the beginning of everyday work on the formation of a position, directly depends on the forms of exposure!

  6. But the implementation of the planned planned tasks creates the need to include a number of extraordinary measures in the production plan, taking into account the complex of distribution of internal reserves and resources. It is difficult to say why actively developing third world countries are ambiguous and will be published.

  7. There is something to think about: representatives of modern social reserves urge us to new achievements, which, in turn, should be combined into entire clusters of their own kind. Suddenly, many well-known personalities, which are a vivid example of a continental-European type of political culture, will be associated with industries.

  8. There is something to think about: the key features of the structure of the project are verified in a timely manner. The opposite point of view implies that the shareholders of the largest companies, overcoming the current economic situation, are indicated as applicants for the role of key factors.

  9. As well as direct participants in technological progress calls us to new achievements, which, in turn, must be exposed. Our business is not as unambiguous as it might seem: the constant information and propaganda support of our activities leaves no chance for clustering efforts.

  10. Given the current international situation, the framework of training allows us to evaluate the value of the output of current assets. A high level of involvement of representatives of the target audience is a clear evidence of a simple fact: the high quality of positional research leaves no chance to prioritize the mind over emotions.

Leave a Reply

Your email address will not be published. Required fields are marked *