Skip to contents

harp uses classes to dispatch data to the correct methods when running a function. as_harp_df takes a data frame and scans the column names and types to assign the correct classes. If the input data frame is not a tibble it will be coerced into one to ensure an easier to read print method.

Usage

as_harp_df(x)

is_harp_df(x)

Arguments

x

a data frame with a valid_dttm column.

Value

a data frame with the appropriate class

Examples

library(dplyr)
#> 
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#> 
#>     filter, lag
#> The following objects are masked from ‘package:base’:
#> 
#>     intersect, setdiff, setequal, union
d_f <- data.frame(valid_dttm = as_dttm(seq_dttm(2021010100, 2021010123)))
as_harp_df(d_f)
#> # A tibble: 24 × 1
#>    valid_dttm         
#>  * <dttm>             
#>  1 2021-01-01 00:00:00
#>  2 2021-01-01 01:00:00
#>  3 2021-01-01 02:00:00
#>  4 2021-01-01 03:00:00
#>  5 2021-01-01 04:00:00
#>  6 2021-01-01 05:00:00
#>  7 2021-01-01 06:00:00
#>  8 2021-01-01 07:00:00
#>  9 2021-01-01 08:00:00
#> 10 2021-01-01 09:00:00
#> # ℹ 14 more rows
as_harp_df(mutate(d_f, fcst_det = runif(24)))
#> ::deterministic point forecast:: # A tibble: 24 × 3
#>    fcst_model valid_dttm            fcst
#>  * <chr>      <dttm>               <dbl>
#>  1 fcst       2021-01-01 00:00:00 0.199 
#>  2 fcst       2021-01-01 01:00:00 0.157 
#>  3 fcst       2021-01-01 02:00:00 0.544 
#>  4 fcst       2021-01-01 03:00:00 0.729 
#>  5 fcst       2021-01-01 04:00:00 0.786 
#>  6 fcst       2021-01-01 05:00:00 0.0539
#>  7 fcst       2021-01-01 06:00:00 0.683 
#>  8 fcst       2021-01-01 07:00:00 0.567 
#>  9 fcst       2021-01-01 08:00:00 0.726 
#> 10 fcst       2021-01-01 09:00:00 0.273 
#> # ℹ 14 more rows
as_harp_df(mutate(d_f, fcst_mbr000 = runif(24), fcst_mbr001 = runif(24)))
#> ::ensemble point forecast:: # A tibble: 24 × 3
#>    valid_dttm          fcst_mbr000 fcst_mbr001
#>  * <dttm>                    <dbl>       <dbl>
#>  1 2021-01-01 00:00:00      0.386      0.381  
#>  2 2021-01-01 01:00:00      0.103      0.942  
#>  3 2021-01-01 02:00:00      0.916      0.567  
#>  4 2021-01-01 03:00:00      0.443      0.00178
#>  5 2021-01-01 04:00:00      0.507      0.212  
#>  6 2021-01-01 05:00:00      0.781      0.658  
#>  7 2021-01-01 06:00:00      0.275      0.883  
#>  8 2021-01-01 07:00:00      0.240      0.747  
#>  9 2021-01-01 08:00:00      0.0586     0.286  
#> 10 2021-01-01 09:00:00      0.894      0.819  
#> # ℹ 14 more rows
# Note the class
class(as_harp_df(d_f))
#> [1] "harp_point_df" "harp_df"       "tbl_df"        "tbl"          
#> [5] "data.frame"   
class(as_harp_df(mutate(d_f, fcst_det = runif(24))))
#> [1] "harp_det_point_df" "harp_point_df"     "harp_df"          
#> [4] "tbl_df"            "tbl"               "data.frame"       
class(as_harp_df(mutate(d_f, fcst_mbr000 = runif(24), fcst_mbr001 = runif(24))))
#> [1] "harp_ens_point_df" "harp_point_df"     "harp_df"          
#> [4] "tbl_df"            "tbl"               "data.frame"