convert_unit() of a vector or convert_unit_at() specific columns of a dataframe.

convert_unit(x, from, to)

convert_unit_at(x, .at, from, to)

Arguments

x

A vector or dataframe.

from

the unit in which the measurement was made.

to

the unit to which the measurement is to be converted.

.at

A character vector of names, positive numeric vector of positions to include, or a negative numeric vector of positions to exlude. Only those elements corresponding to .at will be modified. If the tidyselect package is installed, you can use vars() and the tidyselect helpers to select elements.

Value

An object with the same structure as x (vector or dataframe).

See also

measurements::conv_unit(), purrr::map_at().

Other general functions to perform common transforms: standardize_at

Examples

# For all units see ?measurements::conv_unit() convert_unit(10, "mm2", "hectare")
#> [1] 1e-09
convert_unit(1:3, from = "m", to = "mm")
#> [1] 1000 2000 3000
convert_unit(1:3, "mph", "kph")
#> [1] 1.609344 3.218688 4.828032
dfm <- data.frame(dbh = c(10, 100), name = c(10, 100)) convert_unit_at(dfm, .at = "dbh", from = "mm2", to = "hectare")
#> dbh name #> 1 1e-09 10 #> 2 1e-08 100
# All columns convert_unit_at(dfm, .at = c("dbh", "name"), from = "mm2", to = "hectare")
#> dbh name #> 1 1e-09 1e-09 #> 2 1e-08 1e-08
# Same convert_unit_at(dfm, .at = names(dfm), from = "mm2", to = "hectare")
#> dbh name #> 1 1e-09 1e-09 #> 2 1e-08 1e-08