Skip to contents

expand_date extracts the year, month, day, hour and minute from a date-time column in a data frame and creates a column for each part of the date. If the name of the date-time column ends with "date", the names of the new columns are whatever precedes "date" in the original column name followed by "_year", "_month", "_day", "_hour", "_minute". If the original column name does not end with date the suffixes are pasted onto the original column name.

Usage

expand_date(.data, col, text_months = FALSE)

Arguments

.data

A data frame or harp_list of data frames

col

The name of the date-time to column to be expanded. Can be quoted or unquoted. If using a variable, it should be wrapped in .

text_months

Logical. If TRUE, month names are used rather than numbers.

Value

A data frame or harp_list of data frames with new columns for the expanded date

Examples

expand_date(det_point_df, fcst_dttm)
#> ::deterministic point forecast:: # A tibble: 48 × 11
#>    fcst_model fcst_dttm           lead_time valid_dttm            SID  fcst
#>    <chr>      <dttm>                  <dbl> <dttm>              <dbl> <dbl>
#>  1 point      2021-01-01 00:00:00         0 2021-01-01 00:00:00  1001 0.300
#>  2 point      2021-01-01 00:00:00         1 2021-01-01 01:00:00  1001 0.611
#>  3 point      2021-01-01 00:00:00         2 2021-01-01 02:00:00  1001 0.802
#>  4 point      2021-01-01 00:00:00         3 2021-01-01 03:00:00  1001 0.361
#>  5 point      2021-01-01 00:00:00         4 2021-01-01 04:00:00  1001 0.213
#>  6 point      2021-01-01 00:00:00         5 2021-01-01 05:00:00  1001 0.736
#>  7 point      2021-01-01 00:00:00         6 2021-01-01 06:00:00  1001 0.177
#>  8 point      2021-01-01 00:00:00         7 2021-01-01 07:00:00  1001 0.866
#>  9 point      2021-01-01 00:00:00         8 2021-01-01 08:00:00  1001 0.109
#> 10 point      2021-01-01 00:00:00         9 2021-01-01 09:00:00  1001 0.436
#> # ℹ 38 more rows
#> # ℹ 5 more variables: fcst_year <int>, fcst_month <int>, fcst_day <int>,
#> #   fcst_hour <int>, fcst_minute <int>
expand_date(det_point_list, valid_dttm)
#>  a
#> ::deterministic point forecast:: # A tibble: 48 × 11
#>    fcst_model fcst_dttm           lead_time valid_dttm            SID   fcst
#>    <chr>      <dttm>                  <dbl> <dttm>              <dbl>  <dbl>
#>  1 a          2021-01-01 00:00:00         0 2021-01-01 00:00:00  1001 0.254 
#>  2 a          2021-01-01 00:00:00         1 2021-01-01 01:00:00  1001 0.0506
#>  3 a          2021-01-01 00:00:00         2 2021-01-01 02:00:00  1001 0.236 
#>  4 a          2021-01-01 00:00:00         3 2021-01-01 03:00:00  1001 0.298 
#>  5 a          2021-01-01 00:00:00         4 2021-01-01 04:00:00  1001 0.467 
#>  6 a          2021-01-01 00:00:00         5 2021-01-01 05:00:00  1001 0.376 
#>  7 a          2021-01-01 00:00:00         6 2021-01-01 06:00:00  1001 0.217 
#>  8 a          2021-01-01 00:00:00         7 2021-01-01 07:00:00  1001 0.696 
#>  9 a          2021-01-01 00:00:00         8 2021-01-01 08:00:00  1001 0.227 
#> 10 a          2021-01-01 00:00:00         9 2021-01-01 09:00:00  1001 0.359 
#> # ℹ 38 more rows
#> # ℹ 5 more variables: valid_year <int>, valid_month <int>, valid_day <int>,
#> #   valid_hour <int>, valid_minute <int>
#> 
#>  b
#> ::deterministic point forecast:: # A tibble: 48 × 11
#>    fcst_model fcst_dttm           lead_time valid_dttm            SID  fcst
#>    <chr>      <dttm>                  <dbl> <dttm>              <dbl> <dbl>
#>  1 b          2021-01-01 00:00:00         0 2021-01-01 00:00:00  1001 0.746
#>  2 b          2021-01-01 00:00:00         1 2021-01-01 01:00:00  1001 0.409
#>  3 b          2021-01-01 00:00:00         2 2021-01-01 02:00:00  1001 0.484
#>  4 b          2021-01-01 00:00:00         3 2021-01-01 03:00:00  1001 0.677
#>  5 b          2021-01-01 00:00:00         4 2021-01-01 04:00:00  1001 0.730
#>  6 b          2021-01-01 00:00:00         5 2021-01-01 05:00:00  1001 0.413
#>  7 b          2021-01-01 00:00:00         6 2021-01-01 06:00:00  1001 0.689
#>  8 b          2021-01-01 00:00:00         7 2021-01-01 07:00:00  1001 0.430
#>  9 b          2021-01-01 00:00:00         8 2021-01-01 08:00:00  1001 0.720
#> 10 b          2021-01-01 00:00:00         9 2021-01-01 09:00:00  1001 0.194
#> # ℹ 38 more rows
#> # ℹ 5 more variables: valid_year <int>, valid_month <int>, valid_day <int>,
#> #   valid_hour <int>, valid_minute <int>
#> 
expand_date(ens_point_df, valid_dttm, text_months = TRUE)
#> ::ensemble point forecast:: # A tibble: 48 × 11
#>    fcst_dttm           lead_time valid_dttm            SID point_mbr000
#>    <dttm>                  <dbl> <dttm>              <dbl>        <dbl>
#>  1 2021-01-01 00:00:00         0 2021-01-01 00:00:00  1001       0.277 
#>  2 2021-01-01 00:00:00         1 2021-01-01 01:00:00  1001       0.650 
#>  3 2021-01-01 00:00:00         2 2021-01-01 02:00:00  1001       0.601 
#>  4 2021-01-01 00:00:00         3 2021-01-01 03:00:00  1001       0.427 
#>  5 2021-01-01 00:00:00         4 2021-01-01 04:00:00  1001       0.0798
#>  6 2021-01-01 00:00:00         5 2021-01-01 05:00:00  1001       0.762 
#>  7 2021-01-01 00:00:00         6 2021-01-01 06:00:00  1001       0.347 
#>  8 2021-01-01 00:00:00         7 2021-01-01 07:00:00  1001       0.488 
#>  9 2021-01-01 00:00:00         8 2021-01-01 08:00:00  1001       0.438 
#> 10 2021-01-01 00:00:00         9 2021-01-01 09:00:00  1001       0.662 
#> # ℹ 38 more rows
#> # ℹ 6 more variables: point_mbr001 <dbl>, valid_year <int>, valid_month <chr>,
#> #   valid_day <int>, valid_hour <int>, valid_minute <int>

# Column name can be quoted
expand_date(ens_grid_df, "fcst_dttm")
#> ::ensemble gridded forecast:: # A tibble: 24 × 10
#>    fcst_dttm           lead_time valid_dttm          grid_mbr000 grid_mbr001
#>    <dttm>                  <dbl> <dttm>                <geolist>   <geolist>
#>  1 2021-01-01 00:00:00         0 2021-01-01 00:00:00     [5 × 5]     [5 × 5]
#>  2 2021-01-01 00:00:00         1 2021-01-01 01:00:00     [5 × 5]     [5 × 5]
#>  3 2021-01-01 00:00:00         2 2021-01-01 02:00:00     [5 × 5]     [5 × 5]
#>  4 2021-01-01 00:00:00         3 2021-01-01 03:00:00     [5 × 5]     [5 × 5]
#>  5 2021-01-01 00:00:00         4 2021-01-01 04:00:00     [5 × 5]     [5 × 5]
#>  6 2021-01-01 00:00:00         5 2021-01-01 05:00:00     [5 × 5]     [5 × 5]
#>  7 2021-01-01 00:00:00         6 2021-01-01 06:00:00     [5 × 5]     [5 × 5]
#>  8 2021-01-01 00:00:00         7 2021-01-01 07:00:00     [5 × 5]     [5 × 5]
#>  9 2021-01-01 00:00:00         8 2021-01-01 08:00:00     [5 × 5]     [5 × 5]
#> 10 2021-01-01 00:00:00         9 2021-01-01 09:00:00     [5 × 5]     [5 × 5]
#> # ℹ 14 more rows
#> # ℹ 5 more variables: fcst_year <int>, fcst_month <int>, fcst_day <int>,
#> #   fcst_hour <int>, fcst_minute <int>

# If using a variable, wrap in {{<var>}}
my_col <- "fcst_dttm"
expand_date(ens_grid_df, {{my_col}})
#> ::ensemble gridded forecast:: # A tibble: 24 × 10
#>    fcst_dttm           lead_time valid_dttm          grid_mbr000 grid_mbr001
#>    <dttm>                  <dbl> <dttm>                <geolist>   <geolist>
#>  1 2021-01-01 00:00:00         0 2021-01-01 00:00:00     [5 × 5]     [5 × 5]
#>  2 2021-01-01 00:00:00         1 2021-01-01 01:00:00     [5 × 5]     [5 × 5]
#>  3 2021-01-01 00:00:00         2 2021-01-01 02:00:00     [5 × 5]     [5 × 5]
#>  4 2021-01-01 00:00:00         3 2021-01-01 03:00:00     [5 × 5]     [5 × 5]
#>  5 2021-01-01 00:00:00         4 2021-01-01 04:00:00     [5 × 5]     [5 × 5]
#>  6 2021-01-01 00:00:00         5 2021-01-01 05:00:00     [5 × 5]     [5 × 5]
#>  7 2021-01-01 00:00:00         6 2021-01-01 06:00:00     [5 × 5]     [5 × 5]
#>  8 2021-01-01 00:00:00         7 2021-01-01 07:00:00     [5 × 5]     [5 × 5]
#>  9 2021-01-01 00:00:00         8 2021-01-01 08:00:00     [5 × 5]     [5 × 5]
#> 10 2021-01-01 00:00:00         9 2021-01-01 09:00:00     [5 × 5]     [5 × 5]
#> # ℹ 14 more rows
#> # ℹ 5 more variables: fcst_year <int>, fcst_month <int>, fcst_day <int>,
#> #   fcst_hour <int>, fcst_minute <int>