Created
February 17, 2022 15:19
-
-
Save ctesta01/493ec8ac2259eded86d6b735e65b7e13 to your computer and use it in GitHub Desktop.
Create year-quarter variables from dates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(dplyr) | |
| library(lubridate) | |
| library(stringr) | |
| # let's say my data has some dates in it -- these are just random dates for an example | |
| # and I want to code them to year and quarter | |
| df <- data.frame( | |
| # random dates between 2020-01-01 and 2022-02-14 | |
| date = lubridate::ymd('2020-01-01') + sample.int(775, 100, replace = TRUE)) | |
| df <- df %>% | |
| mutate( | |
| # extract the year | |
| year = lubridate::year(date), | |
| # create year breaks for each row | |
| year_start = lubridate::ymd(stringr::str_c(year, "-01-01")), | |
| year_q1_end = year_start + round(365/4), | |
| year_q2_end = year_start + round(365/4*2), | |
| year_q3_end = year_start + round(365/4*3), | |
| year_q4_end = lubridate::ymd(stringr::str_c(year, "-12-31")), | |
| # code the quarter | |
| quarter = case_when( | |
| date >= year_start & date <= year_q1_end ~ "Q1", | |
| date > year_q1_end & date <= year_q2_end ~ "Q2", | |
| date > year_q2_end & date <= year_q3_end ~ "Q3", | |
| date > year_q3_end & date <= year_q4_end ~ "Q4"), | |
| # create year_quarter character variable | |
| year_quarter = stringr::str_c(year, "-", quarter), | |
| # code year_quarter to ordered factor variable | |
| year_quarter = factor(year_quarter, | |
| levels = stringr::str_c(rep(2020:2022, each=4), "-", | |
| stringr::str_c("Q", 1:4))) | |
| ) | |
| df %>% arrange(df) %>% | |
| select(date, year_quarter) | |
| ## date year_quarter | |
| ## 1 2020-01-11 2020-Q1 | |
| ## 2 2020-01-11 2020-Q1 | |
| ## 3 2020-01-24 2020-Q1 | |
| ## 4 2020-02-02 2020-Q1 | |
| ## 5 2020-02-04 2020-Q1 | |
| ## 6 2020-02-06 2020-Q1 | |
| ## 7 2020-02-07 2020-Q1 | |
| ## 8 2020-02-10 2020-Q1 | |
| ## 9 2020-02-12 2020-Q1 | |
| ## 10 2020-02-15 2020-Q1 | |
| ## 11 2020-02-19 2020-Q1 | |
| ## 12 2020-03-14 2020-Q1 | |
| ## 13 2020-03-27 2020-Q1 | |
| ## 14 2020-04-19 2020-Q2 | |
| ## 15 2020-04-26 2020-Q2 | |
| ## 16 2020-05-18 2020-Q2 | |
| ## 17 2020-05-20 2020-Q2 | |
| ## 18 2020-05-20 2020-Q2 | |
| ## 19 2020-06-02 2020-Q2 | |
| ## 20 2020-06-22 2020-Q2 | |
| ## 21 2020-06-23 2020-Q2 | |
| ## 22 2020-06-28 2020-Q2 | |
| ## 23 2020-07-14 2020-Q3 | |
| ## 24 2020-07-22 2020-Q3 | |
| ## 25 2020-07-26 2020-Q3 | |
| ## 26 2020-08-08 2020-Q3 | |
| ## 27 2020-09-23 2020-Q3 | |
| ## 28 2020-09-29 2020-Q3 | |
| ## 29 2020-09-30 2020-Q3 | |
| ## 30 2020-10-02 2020-Q4 | |
| ## 31 2020-10-03 2020-Q4 | |
| ## 32 2020-10-07 2020-Q4 | |
| ## 33 2020-10-11 2020-Q4 | |
| ## 34 2020-10-11 2020-Q4 | |
| ## 35 2020-10-25 2020-Q4 | |
| ## 36 2020-10-25 2020-Q4 | |
| ## 37 2020-10-27 2020-Q4 | |
| ## 38 2020-10-29 2020-Q4 | |
| ## 39 2020-10-30 2020-Q4 | |
| ## 40 2020-10-30 2020-Q4 | |
| ## 41 2020-10-31 2020-Q4 | |
| ## 42 2020-11-01 2020-Q4 | |
| ## 43 2020-11-18 2020-Q4 | |
| ## 44 2020-11-24 2020-Q4 | |
| ## 45 2020-11-24 2020-Q4 | |
| ## 46 2020-12-06 2020-Q4 | |
| ## 47 2021-01-05 2021-Q1 | |
| ## 48 2021-02-08 2021-Q1 | |
| ## 49 2021-02-10 2021-Q1 | |
| ## 50 2021-02-15 2021-Q1 | |
| ## 51 2021-02-21 2021-Q1 | |
| ## 52 2021-02-26 2021-Q1 | |
| ## 53 2021-02-26 2021-Q1 | |
| ## 54 2021-02-26 2021-Q1 | |
| ## 55 2021-03-04 2021-Q1 | |
| ## 56 2021-03-10 2021-Q1 | |
| ## 57 2021-03-25 2021-Q1 | |
| ## 58 2021-03-28 2021-Q1 | |
| ## 59 2021-03-30 2021-Q1 | |
| ## 60 2021-04-07 2021-Q2 | |
| ## 61 2021-04-26 2021-Q2 | |
| ## 62 2021-04-30 2021-Q2 | |
| ## 63 2021-05-09 2021-Q2 | |
| ## 64 2021-05-14 2021-Q2 | |
| ## 65 2021-05-16 2021-Q2 | |
| ## 66 2021-05-27 2021-Q2 | |
| ## 67 2021-06-06 2021-Q2 | |
| ## 68 2021-06-10 2021-Q2 | |
| ## 69 2021-06-17 2021-Q2 | |
| ## 70 2021-06-25 2021-Q2 | |
| ## 71 2021-06-28 2021-Q2 | |
| ## 72 2021-07-02 2021-Q2 | |
| ## 73 2021-07-05 2021-Q3 | |
| ## 74 2021-07-05 2021-Q3 | |
| ## 75 2021-07-16 2021-Q3 | |
| ## 76 2021-07-17 2021-Q3 | |
| ## 77 2021-07-18 2021-Q3 | |
| ## 78 2021-07-30 2021-Q3 | |
| ## 79 2021-08-02 2021-Q3 | |
| ## 80 2021-08-17 2021-Q3 | |
| ## 81 2021-08-25 2021-Q3 | |
| ## 82 2021-08-29 2021-Q3 | |
| ## 83 2021-08-31 2021-Q3 | |
| ## 84 2021-08-31 2021-Q3 | |
| ## 85 2021-09-14 2021-Q3 | |
| ## 86 2021-09-15 2021-Q3 | |
| ## 87 2021-09-20 2021-Q3 | |
| ## 88 2021-09-25 2021-Q3 | |
| ## 89 2021-09-27 2021-Q3 | |
| ## 90 2021-10-21 2021-Q4 | |
| ## 91 2021-10-22 2021-Q4 | |
| ## 92 2021-10-30 2021-Q4 | |
| ## 93 2021-11-05 2021-Q4 | |
| ## 94 2021-11-24 2021-Q4 | |
| ## 95 2021-11-26 2021-Q4 | |
| ## 96 2021-11-30 2021-Q4 | |
| ## 97 2022-01-02 2022-Q1 | |
| ## 98 2022-01-09 2022-Q1 | |
| ## 99 2022-01-13 2022-Q1 | |
| ## 100 2022-02-01 2022-Q1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment