Skip to contents

This function is intended for data frames / tibbles only and makes the specified columns explicit with the target units in the column name.

Usage

make_units_explicit(
  df,
  ...,
  prefix = paste0(" ", group[1]),
  suffix = group[2],
  group = units_options("group")
)

Arguments

df

the data frame in which to make the units explicit

...

which columns to make explicit ([dplyr::select())] syntax). If none provided, makes all columns explicit.

[dplyr::select())]: R:dplyr::select())

prefix

the prefix text for the units. Default: " [" (based on units::units_options()).

suffix

the suffix text for the units. Default: "]" (based on units::units_options()).

group

parameter to specify the prefix and suffix text as a vector of length 2. This is helpful because it allows compatiblity with the units::units_options() group option.

Value

a text representation of the units

Examples

# data frame with quantities
df <- tibble(
  weight = set_cu(1:5, "mg"),
  vol = set_cu(20, "mL"),
  mw = set_cu(500, "g/mol"),
  amount = weight / mw,
  conc = amount / vol
)

# make all units columns explicit
df |> make_units_explicit()
#> # A tibble: 5 × 5
#>   `weight [mg]` `vol [mL]` `mw [g/mol]` `amount [µmol]` `conc [µM]`
#>           <dbl>      <dbl>        <dbl>           <dbl>       <dbl>
#> 1             1         20          500               2         100
#> 2             2         20          500               4         200
#> 3             3         20          500               6         300
#> 4             4         20          500               8         400
#> 5             5         20          500              10         500

# change the prefix and suffix
df |> make_units_explicit(prefix = ".", suffix = "")
#> # A tibble: 5 × 5
#>   weight.mg vol.mL `mw.g/mol` amount.µmol conc.µM
#>       <dbl>  <dbl>      <dbl>       <dbl>   <dbl>
#> 1         1     20        500           2     100
#> 2         2     20        500           4     200
#> 3         3     20        500           6     300
#> 4         4     20        500           8     400
#> 5         5     20        500          10     500

# make a specific subset of columns explicit (using tidy select syntax)
df |> make_units_explicit(weight:mw) # from weight to mw
#> # A tibble: 5 × 5
#>   `weight [mg]` `vol [mL]` `mw [g/mol]` amount conc
#>           <dbl>      <dbl>        <dbl> [µmol] [µM]
#> 1             1         20          500      2  100
#> 2             2         20          500      4  200
#> 3             3         20          500      6  300
#> 4             4         20          500      8  400
#> 5             5         20          500     10  500
df |> make_units_explicit(-mw) # all but mw
#> # A tibble: 5 × 5
#>   `weight [mg]` `vol [mL]`      mw `amount [µmol]` `conc [µM]`
#>           <dbl>      <dbl> [g/mol]           <dbl>       <dbl>
#> 1             1         20     500               2         100
#> 2             2         20     500               4         200
#> 3             3         20     500               6         300
#> 4             4         20     500               8         400
#> 5             5         20     500              10         500