Skip to contents

Multiple variables to unique date variable [Experimental]

Usage

vars_to_date(
  tbl,
  year = NULL,
  quarter = NULL,
  month = NULL,
  day = NULL,
  date = NULL,
  drop_vars = TRUE,
  clean_names = FALSE,
  date_format = "%d-%m-%y",
  origin = "1900-01-01",
  .round = c("end", "middle", "start")
)

Arguments

tbl

data.frame or tbl connection

year

year variable position or name

quarter

quarter variable position or name

month

month variable position or name

day

day variable position or name

date

a variable name or position containing a like date format

drop_vars

indicates if variables should be dropped

clean_names

indicates if all variable names should be cleaned

date_format

actual date format of variable in date argument

origin

base date for variable convertion to date

.round

indicates if the date should be rounded to the end, middle or start of the period

Value

tbl a new data.frame with the compute variable

Examples

tbl <- data.frame(
  year = rep("2021", 12),
  month = month.name,
  day = sample(1:3, 12, TRUE),
  value = sample(100:1000, 12, TRUE)
)

tbl
#>    year     month day value
#> 1  2021   January   1   866
#> 2  2021  February   3   635
#> 3  2021     March   3   946
#> 4  2021     April   1   176
#> 5  2021       May   2   997
#> 6  2021      June   3   545
#> 7  2021      July   3   666
#> 8  2021    August   3   526
#> 9  2021 September   2   161
#> 10 2021   October   1   654
#> 11 2021  November   1   872
#> 12 2021  December   1   568

vars_to_date(tbl, year = 1, month = 2, day = 3)
#>          date value
#> 1  2021-01-01   866
#> 2  2021-02-03   635
#> 3  2021-03-03   946
#> 4  2021-04-01   176
#> 5  2021-05-02   997
#> 6  2021-06-03   545
#> 7  2021-07-03   666
#> 8  2021-08-03   526
#> 9  2021-09-02   161
#> 10 2021-10-01   654
#> 11 2021-11-01   872
#> 12 2021-12-01   568

# and supports various frequencies and date formats

tbl <- data.frame(
  year = rep("2021", 12),
  quarter = sample(
            c(
              "Enero-Marzo",
              "Abril-Junio",
              "Julio-Septiembre",
              "Octubre-Diciembre"
             ),
            12,
            TRUE
           ),
  value = sample(100:1000, 12, TRUE)
)

tbl
#>    year           quarter value
#> 1  2021 Octubre-Diciembre   654
#> 2  2021       Enero-Marzo   643
#> 3  2021       Abril-Junio   169
#> 4  2021       Abril-Junio   364
#> 5  2021  Julio-Septiembre   249
#> 6  2021  Julio-Septiembre   365
#> 7  2021 Octubre-Diciembre   515
#> 8  2021 Octubre-Diciembre   216
#> 9  2021       Abril-Junio   628
#> 10 2021  Julio-Septiembre   279
#> 11 2021       Enero-Marzo   283
#> 12 2021       Abril-Junio   121

vars_to_date(tbl, year = 1, quarter = 2)
#>          date value
#> 1  2021-12-31   654
#> 2  2021-03-31   643
#> 3  2021-06-30   169
#> 4  2021-06-30   364
#> 5  2021-09-30   249
#> 6  2021-09-30   365
#> 7  2021-12-31   515
#> 8  2021-12-31   216
#> 9  2021-06-30   628
#> 10 2021-09-30   279
#> 11 2021-03-31   283
#> 12 2021-06-30   121