Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wilsonfreitas/666d743ad7621fb28126038b2b964041 to your computer and use it in GitHub Desktop.
Save wilsonfreitas/666d743ad7621fb28126038b2b964041 to your computer and use it in GitHub Desktop.
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Untitled0.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyNsy4RaWWJJXRN8opuCmESy",
"include_colab_link": true
},
"kernelspec": {
"name": "ir",
"display_name": "R"
},
"language_info": {
"name": "R"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/wilsonfreitas/666d743ad7621fb28126038b2b964041/untitled0.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "2GG_SyyQcKMA",
"outputId": "18133957-ceeb-47a7-98c0-93a67e4c2eba"
},
"source": [
"library(tidyverse)"
],
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": [
"Warning message in system(\"timedatectl\", intern = TRUE):\n",
"“running command 'timedatectl' had status 1”\n",
"── \u001b[1mAttaching packages\u001b[22m ─────────────────────────────────────── tidyverse 1.3.1 ──\n",
"\n",
"\u001b[32m✔\u001b[39m \u001b[34mggplot2\u001b[39m 3.3.5 \u001b[32m✔\u001b[39m \u001b[34mpurrr \u001b[39m 0.3.4\n",
"\u001b[32m✔\u001b[39m \u001b[34mtibble \u001b[39m 3.1.3 \u001b[32m✔\u001b[39m \u001b[34mdplyr \u001b[39m 1.0.7\n",
"\u001b[32m✔\u001b[39m \u001b[34mtidyr \u001b[39m 1.1.3 \u001b[32m✔\u001b[39m \u001b[34mstringr\u001b[39m 1.4.0\n",
"\u001b[32m✔\u001b[39m \u001b[34mreadr \u001b[39m 2.0.1 \u001b[32m✔\u001b[39m \u001b[34mforcats\u001b[39m 0.5.1\n",
"\n",
"── \u001b[1mConflicts\u001b[22m ────────────────────────────────────────── tidyverse_conflicts() ──\n",
"\u001b[31m✖\u001b[39m \u001b[34mdplyr\u001b[39m::\u001b[32mfilter()\u001b[39m masks \u001b[34mstats\u001b[39m::filter()\n",
"\u001b[31m✖\u001b[39m \u001b[34mdplyr\u001b[39m::\u001b[32mlag()\u001b[39m masks \u001b[34mstats\u001b[39m::lag()\n",
"\n"
],
"name": "stderr"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "1GFJ6zsglVK9",
"outputId": "aed7e87a-8ed0-4d43-cb71-7518f3b8eecb"
},
"source": [
"library(bizdays)"
],
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"text": [
"\n",
"Attaching package: ‘bizdays’\n",
"\n",
"\n",
"The following object is masked from ‘package:stats’:\n",
"\n",
" offset\n",
"\n",
"\n"
],
"name": "stderr"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "9CSyAH98k-Rm"
},
"source": [
"free <- c(\"2021-01-01\", \"2021-01-06\", \"2021-04-04\", \"2021-04-05\", \"2021-05-06\", \"2021-05-03\",\n",
" \"2021-05-23\", \"2021-06-03\", \"2021-08-15\", \"2021-11-01\", \"2021-11-11\",\n",
" \"2021-12-30\", \"2021-12-26\") |> as.Date()\n"
],
"execution_count": 6,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "c8mEKPu5lD2p"
},
"source": [
"create.calendar(name=\"pol\",\n",
" holidays = free,\n",
" weekdays=c(\"saturday\", \"sunday\"),\n",
" start.date = as.Date(\"2021-01-01\"),\n",
" end.date = as.Date(\"2021-12-31\"),\n",
" financial = FALSE)\n"
],
"execution_count": 26,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "fC0w3h6IlqwN",
"outputId": "03a7e873-bd4a-4fd5-ebc3-40aa7e706e67"
},
"source": [
"library(lubridate)"
],
"execution_count": 13,
"outputs": [
{
"output_type": "stream",
"text": [
"\n",
"Attaching package: ‘lubridate’\n",
"\n",
"\n",
"The following objects are masked from ‘package:base’:\n",
"\n",
" date, intersect, setdiff, union\n",
"\n",
"\n"
],
"name": "stderr"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 136
},
"id": "w6J-mkFdlyb0",
"outputId": "8aa7957f-4d00-4bec-c4c9-f74a57611d84"
},
"source": [
"calendars()[[\"pol\"]]"
],
"execution_count": 27,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"pol non financial calendar \n",
" 13 holidays \n",
" 2 weekdays (saturday, sunday) \n",
" range from 2021-01-01 to 2021-12-31 \n",
"bizdays arguments adjust\n",
" from: none \n",
" to: none "
]
},
"metadata": {}
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "aH8LFQwopU9p"
},
"source": [
"bizdays2 <- function(from, to, cal) {\n",
" x <- \\(from, to) seq(from, to, by = \"day\") |> is.bizday(cal) |> sum()\n",
" mapply(x, from, to)\n",
"}"
],
"execution_count": 48,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 469
},
"id": "DyDGaknAmXk0",
"outputId": "45262955-8260-40ef-b1e1-e0f9b6955f38"
},
"source": [
"tibble(\n",
" from = ymd(paste0(\"2021-\", 1:12, \"-01\")),\n",
" to = lead(from, default = as.Date(\"2021-12-31\"))-1,\n",
" bizdays = bizdays(from = from, to = to, \"pol\"),\n",
" bizdays2 = bizdays2(from, to, \"pol\"),\n",
" check = bizdays == bizdays2)\n"
],
"execution_count": 50,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
" from to bizdays bizdays2 check\n",
"1 2021-01-01 2021-01-31 19 19 TRUE \n",
"2 2021-02-01 2021-02-28 20 20 TRUE \n",
"3 2021-03-01 2021-03-31 23 23 TRUE \n",
"4 2021-04-01 2021-04-30 21 21 TRUE \n",
"5 2021-05-01 2021-05-31 19 19 TRUE \n",
"6 2021-06-01 2021-06-30 21 21 TRUE \n",
"7 2021-07-01 2021-07-31 22 22 TRUE \n",
"8 2021-08-01 2021-08-31 22 22 TRUE \n",
"9 2021-09-01 2021-09-30 22 22 TRUE \n",
"10 2021-10-01 2021-10-31 21 21 TRUE \n",
"11 2021-11-01 2021-11-30 20 20 TRUE \n",
"12 2021-12-01 2021-12-30 21 21 TRUE "
],
"text/latex": "A tibble: 12 × 5\n\\begin{tabular}{lllll}\n from & to & bizdays & bizdays2 & check\\\\\n <date> & <date> & <dbl> & <int> & <lgl>\\\\\n\\hline\n\t 2021-01-01 & 2021-01-31 & 19 & 19 & TRUE\\\\\n\t 2021-02-01 & 2021-02-28 & 20 & 20 & TRUE\\\\\n\t 2021-03-01 & 2021-03-31 & 23 & 23 & TRUE\\\\\n\t 2021-04-01 & 2021-04-30 & 21 & 21 & TRUE\\\\\n\t 2021-05-01 & 2021-05-31 & 19 & 19 & TRUE\\\\\n\t 2021-06-01 & 2021-06-30 & 21 & 21 & TRUE\\\\\n\t 2021-07-01 & 2021-07-31 & 22 & 22 & TRUE\\\\\n\t 2021-08-01 & 2021-08-31 & 22 & 22 & TRUE\\\\\n\t 2021-09-01 & 2021-09-30 & 22 & 22 & TRUE\\\\\n\t 2021-10-01 & 2021-10-31 & 21 & 21 & TRUE\\\\\n\t 2021-11-01 & 2021-11-30 & 20 & 20 & TRUE\\\\\n\t 2021-12-01 & 2021-12-30 & 21 & 21 & TRUE\\\\\n\\end{tabular}\n",
"text/markdown": "\nA tibble: 12 × 5\n\n| from &lt;date&gt; | to &lt;date&gt; | bizdays &lt;dbl&gt; | bizdays2 &lt;int&gt; | check &lt;lgl&gt; |\n|---|---|---|---|---|\n| 2021-01-01 | 2021-01-31 | 19 | 19 | TRUE |\n| 2021-02-01 | 2021-02-28 | 20 | 20 | TRUE |\n| 2021-03-01 | 2021-03-31 | 23 | 23 | TRUE |\n| 2021-04-01 | 2021-04-30 | 21 | 21 | TRUE |\n| 2021-05-01 | 2021-05-31 | 19 | 19 | TRUE |\n| 2021-06-01 | 2021-06-30 | 21 | 21 | TRUE |\n| 2021-07-01 | 2021-07-31 | 22 | 22 | TRUE |\n| 2021-08-01 | 2021-08-31 | 22 | 22 | TRUE |\n| 2021-09-01 | 2021-09-30 | 22 | 22 | TRUE |\n| 2021-10-01 | 2021-10-31 | 21 | 21 | TRUE |\n| 2021-11-01 | 2021-11-30 | 20 | 20 | TRUE |\n| 2021-12-01 | 2021-12-30 | 21 | 21 | TRUE |\n\n",
"text/html": [
"<table class=\"dataframe\">\n",
"<caption>A tibble: 12 × 5</caption>\n",
"<thead>\n",
"\t<tr><th scope=col>from</th><th scope=col>to</th><th scope=col>bizdays</th><th scope=col>bizdays2</th><th scope=col>check</th></tr>\n",
"\t<tr><th scope=col>&lt;date&gt;</th><th scope=col>&lt;date&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;int&gt;</th><th scope=col>&lt;lgl&gt;</th></tr>\n",
"</thead>\n",
"<tbody>\n",
"\t<tr><td>2021-01-01</td><td>2021-01-31</td><td>19</td><td>19</td><td>TRUE</td></tr>\n",
"\t<tr><td>2021-02-01</td><td>2021-02-28</td><td>20</td><td>20</td><td>TRUE</td></tr>\n",
"\t<tr><td>2021-03-01</td><td>2021-03-31</td><td>23</td><td>23</td><td>TRUE</td></tr>\n",
"\t<tr><td>2021-04-01</td><td>2021-04-30</td><td>21</td><td>21</td><td>TRUE</td></tr>\n",
"\t<tr><td>2021-05-01</td><td>2021-05-31</td><td>19</td><td>19</td><td>TRUE</td></tr>\n",
"\t<tr><td>2021-06-01</td><td>2021-06-30</td><td>21</td><td>21</td><td>TRUE</td></tr>\n",
"\t<tr><td>2021-07-01</td><td>2021-07-31</td><td>22</td><td>22</td><td>TRUE</td></tr>\n",
"\t<tr><td>2021-08-01</td><td>2021-08-31</td><td>22</td><td>22</td><td>TRUE</td></tr>\n",
"\t<tr><td>2021-09-01</td><td>2021-09-30</td><td>22</td><td>22</td><td>TRUE</td></tr>\n",
"\t<tr><td>2021-10-01</td><td>2021-10-31</td><td>21</td><td>21</td><td>TRUE</td></tr>\n",
"\t<tr><td>2021-11-01</td><td>2021-11-30</td><td>20</td><td>20</td><td>TRUE</td></tr>\n",
"\t<tr><td>2021-12-01</td><td>2021-12-30</td><td>21</td><td>21</td><td>TRUE</td></tr>\n",
"</tbody>\n",
"</table>\n"
]
},
"metadata": {}
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "s-kEr0LJnZJg"
},
"source": [
""
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment