Scale a parameter in a data frame
Usage
scale_param(x, scaling, new_units, mult = FALSE, ...)
# S3 method for data.frame
scale_param(x, scaling, new_units, mult = FALSE, col, ...)
Arguments
- x
A data frame or a harp_list.
- scaling
The scaling to apply to the data. By default the scaling is additive, but if
mult = TRUE
it is multiplicative.- new_units
The name of the new units. If missing, the units name will be unchanged.
- mult
Logical. Whether the scaling is multiplicative. The default is
FALSE
, meaning that the scaling is additive.- ...
Used by methods.
- col
The name of the column to scale - if
x
is a forecast or analysisharp_df
data frame or aharp_list
, the columns will be selected automatically andcol
is ignored.
Examples
# Make a data frame of 2m temperature observations in degrees C
library(tibble)
obs <- tibble(
valid_date = rep(seq_dttm(2022081500, 2022081523), 3),
SID = c(rep(1001, 24), rep(1002, 24), rep(1003, 24)),
units = "degC",
T2m = rnorm(24 * 3, 15, 2)
)
# Scale to be in Kelvin
scale_param(obs, 273.15, "K", col = T2m)
#> # A tibble: 72 × 4
#> valid_date SID units T2m
#> <chr> <dbl> <chr> <dbl>
#> 1 2022081500 1001 K 287.
#> 2 2022081501 1001 K 290.
#> 3 2022081502 1001 K 290.
#> 4 2022081503 1001 K 284.
#> 5 2022081504 1001 K 284.
#> 6 2022081505 1001 K 285.
#> 7 2022081506 1001 K 291.
#> 8 2022081507 1001 K 286.
#> 9 2022081508 1001 K 287.
#> 10 2022081509 1001 K 290.
#> # ℹ 62 more rows
# col can be a quoted, or if a variable is must be wrapped in {{}}
scale_param(obs, 273.15, "K", col = "T2m")
#> # A tibble: 72 × 4
#> valid_date SID units T2m
#> <chr> <dbl> <chr> <dbl>
#> 1 2022081500 1001 K 287.
#> 2 2022081501 1001 K 290.
#> 3 2022081502 1001 K 290.
#> 4 2022081503 1001 K 284.
#> 5 2022081504 1001 K 284.
#> 6 2022081505 1001 K 285.
#> 7 2022081506 1001 K 291.
#> 8 2022081507 1001 K 286.
#> 9 2022081508 1001 K 287.
#> 10 2022081509 1001 K 290.
#> # ℹ 62 more rows
prm <- "T2m"
scale_param(obs, 273.15, "K", col = {{prm}})
#> # A tibble: 72 × 4
#> valid_date SID units T2m
#> <chr> <dbl> <chr> <dbl>
#> 1 2022081500 1001 K 287.
#> 2 2022081501 1001 K 290.
#> 3 2022081502 1001 K 290.
#> 4 2022081503 1001 K 284.
#> 5 2022081504 1001 K 284.
#> 6 2022081505 1001 K 285.
#> 7 2022081506 1001 K 291.
#> 8 2022081507 1001 K 286.
#> 9 2022081508 1001 K 287.
#> 10 2022081509 1001 K 290.
#> # ℹ 62 more rows
# For forecast data frames, col is not needed
scale_param(det_point_df, 273.15, "K")
#> ::deterministic point forecast:: # A tibble: 48 × 7
#> fcst_model fcst_dttm lead_time valid_dttm SID units
#> <chr> <dttm> <dbl> <dttm> <dbl> <chr>
#> 1 point 2021-01-01 00:00:00 0 2021-01-01 00:00:00 1001 K
#> 2 point 2021-01-01 00:00:00 1 2021-01-01 01:00:00 1001 K
#> 3 point 2021-01-01 00:00:00 2 2021-01-01 02:00:00 1001 K
#> 4 point 2021-01-01 00:00:00 3 2021-01-01 03:00:00 1001 K
#> 5 point 2021-01-01 00:00:00 4 2021-01-01 04:00:00 1001 K
#> 6 point 2021-01-01 00:00:00 5 2021-01-01 05:00:00 1001 K
#> 7 point 2021-01-01 00:00:00 6 2021-01-01 06:00:00 1001 K
#> 8 point 2021-01-01 00:00:00 7 2021-01-01 07:00:00 1001 K
#> 9 point 2021-01-01 00:00:00 8 2021-01-01 08:00:00 1001 K
#> 10 point 2021-01-01 00:00:00 9 2021-01-01 09:00:00 1001 K
#> # ℹ 38 more rows
#> # ℹ 1 more variable: fcst <dbl>
scale_param(ens_point_df, 273.15, "K")
#> ::ensemble point forecast:: # A tibble: 48 × 7
#> fcst_dttm lead_time valid_dttm SID units point_mbr000
#> <dttm> <dbl> <dttm> <dbl> <chr> <dbl>
#> 1 2021-01-01 00:00:00 0 2021-01-01 00:00:00 1001 K 273.
#> 2 2021-01-01 00:00:00 1 2021-01-01 01:00:00 1001 K 274.
#> 3 2021-01-01 00:00:00 2 2021-01-01 02:00:00 1001 K 274.
#> 4 2021-01-01 00:00:00 3 2021-01-01 03:00:00 1001 K 274.
#> 5 2021-01-01 00:00:00 4 2021-01-01 04:00:00 1001 K 273.
#> 6 2021-01-01 00:00:00 5 2021-01-01 05:00:00 1001 K 274.
#> 7 2021-01-01 00:00:00 6 2021-01-01 06:00:00 1001 K 273.
#> 8 2021-01-01 00:00:00 7 2021-01-01 07:00:00 1001 K 274.
#> 9 2021-01-01 00:00:00 8 2021-01-01 08:00:00 1001 K 274.
#> 10 2021-01-01 00:00:00 9 2021-01-01 09:00:00 1001 K 274.
#> # ℹ 38 more rows
#> # ℹ 1 more variable: point_mbr001 <dbl>
# Scaling can be multiplicative
scale_param(det_point_df, 100, "percent", mult = TRUE)
#> ::deterministic point forecast:: # A tibble: 48 × 7
#> fcst_model fcst_dttm lead_time valid_dttm SID units
#> <chr> <dttm> <dbl> <dttm> <dbl> <chr>
#> 1 point 2021-01-01 00:00:00 0 2021-01-01 00:00:00 1001 percent
#> 2 point 2021-01-01 00:00:00 1 2021-01-01 01:00:00 1001 percent
#> 3 point 2021-01-01 00:00:00 2 2021-01-01 02:00:00 1001 percent
#> 4 point 2021-01-01 00:00:00 3 2021-01-01 03:00:00 1001 percent
#> 5 point 2021-01-01 00:00:00 4 2021-01-01 04:00:00 1001 percent
#> 6 point 2021-01-01 00:00:00 5 2021-01-01 05:00:00 1001 percent
#> 7 point 2021-01-01 00:00:00 6 2021-01-01 06:00:00 1001 percent
#> 8 point 2021-01-01 00:00:00 7 2021-01-01 07:00:00 1001 percent
#> 9 point 2021-01-01 00:00:00 8 2021-01-01 08:00:00 1001 percent
#> 10 point 2021-01-01 00:00:00 9 2021-01-01 09:00:00 1001 percent
#> # ℹ 38 more rows
#> # ℹ 1 more variable: fcst <dbl>
scale_param(ens_point_list, 1/1000, "kg/kg", mult = TRUE)
#> • a
#> ::ensemble point forecast:: # A tibble: 48 × 8
#> fcst_model fcst_dttm lead_time valid_dttm SID units
#> <chr> <dttm> <dbl> <dttm> <dbl> <chr>
#> 1 a 2021-01-01 00:00:00 0 2021-01-01 00:00:00 1001 kg/kg
#> 2 a 2021-01-01 00:00:00 1 2021-01-01 01:00:00 1001 kg/kg
#> 3 a 2021-01-01 00:00:00 2 2021-01-01 02:00:00 1001 kg/kg
#> 4 a 2021-01-01 00:00:00 3 2021-01-01 03:00:00 1001 kg/kg
#> 5 a 2021-01-01 00:00:00 4 2021-01-01 04:00:00 1001 kg/kg
#> 6 a 2021-01-01 00:00:00 5 2021-01-01 05:00:00 1001 kg/kg
#> 7 a 2021-01-01 00:00:00 6 2021-01-01 06:00:00 1001 kg/kg
#> 8 a 2021-01-01 00:00:00 7 2021-01-01 07:00:00 1001 kg/kg
#> 9 a 2021-01-01 00:00:00 8 2021-01-01 08:00:00 1001 kg/kg
#> 10 a 2021-01-01 00:00:00 9 2021-01-01 09:00:00 1001 kg/kg
#> # ℹ 38 more rows
#> # ℹ 2 more variables: a_mbr000 <dbl>, a_mbr001 <dbl>
#>
#> • b
#> ::ensemble point forecast:: # A tibble: 48 × 8
#> fcst_model fcst_dttm lead_time valid_dttm SID units
#> <chr> <dttm> <dbl> <dttm> <dbl> <chr>
#> 1 b 2021-01-01 00:00:00 0 2021-01-01 00:00:00 1001 kg/kg
#> 2 b 2021-01-01 00:00:00 1 2021-01-01 01:00:00 1001 kg/kg
#> 3 b 2021-01-01 00:00:00 2 2021-01-01 02:00:00 1001 kg/kg
#> 4 b 2021-01-01 00:00:00 3 2021-01-01 03:00:00 1001 kg/kg
#> 5 b 2021-01-01 00:00:00 4 2021-01-01 04:00:00 1001 kg/kg
#> 6 b 2021-01-01 00:00:00 5 2021-01-01 05:00:00 1001 kg/kg
#> 7 b 2021-01-01 00:00:00 6 2021-01-01 06:00:00 1001 kg/kg
#> 8 b 2021-01-01 00:00:00 7 2021-01-01 07:00:00 1001 kg/kg
#> 9 b 2021-01-01 00:00:00 8 2021-01-01 08:00:00 1001 kg/kg
#> 10 b 2021-01-01 00:00:00 9 2021-01-01 09:00:00 1001 kg/kg
#> # ℹ 38 more rows
#> # ℹ 2 more variables: b_mbr000 <dbl>, b_mbr001 <dbl>
#>