Learn R Programming

dataset (version 0.3.4)

var_unit: Get / Set a unit of measure

Description

Get / Set a unit of measure

Usage

var_unit(x, ...)

var_unit(x) <- value

get_variable_units(x, ...)

unit_attribute(x)

get_unit_attribute(x)

set_unit_attribute(x, value)

unit_attribute(x) <- value

Value

The unit attribute of a vector constructed with defined, or any vector that is enriched with a unit attribute.


The var_unit<-

assignment method allows to add, remove, or overwrite this attribute on a vector x. The assignment function returns the x vector invisibly.

Arguments

x

A vector.

...

Further potential parameters reserved for inherited classes.

value

A character string or NULL to remove the unit of measure.

Details

The aim of the unit attribute is to add to the R vector object its unit of measure (for example, physical units like gram and kilogram or currency units like dollars or euros), so that they are not concatenated or joined in a syntactically correct but semantically incorrect way (i.e., accidentally concatenating values quoted in dollars and euros from different subvectors.) This is particularly useful when working with linked open data, i.e., when joins or concatenations are performed on data arriving from a remote source.

get_variable_units() is identical to var_unit().

See vignette("defined", package = "dataset") to use comprehensively with variable labels, namespaces, units of measures, and machine-independent permanent variable identifiers.

See Also

Other defined metadata methods and functions: defined(), var_label(), var_namespace()

Examples

Run this code
# The defined vector class and dataset_df support units of measure attributes:
var_unit(iris_dataset$Sepal.Length)

# Normally columns of a data.frame do not have a unit attribute:
var_unit(iris$Sepal.Length)

# You can add them with the assignment function:
var_unit(iris$Sepal.Length) <- "centimeter"

# To remove a unit of measure assign the NULL value:
var_unit(iris$Sepal.Length) <- NULL

Run the code above in your browser using DataLab