Skip to contents

Inspect an object's type and measurement metadata:

  • var_type(): Describe the object's storage type or class, optionally using an abbreviation.

  • var_unit(): Report the measurement or duration unit, or NA_character_ when the object has no unit.

Usage

var_type(x, abbr = FALSE)

var_unit(x)

Arguments

x

An object.

abbr

If TRUE, return an abbreviated type. Defaults to FALSE.

Value

A character string. var_type() gives the type description, including "units" for measured vectors in either abbreviation mode. var_unit() gives the unit string for measured or duration vectors, including empty or all-missing measured vectors, and NA_character_ for vectors without units.

Details

Types describe storage and class metadata, not the observed values: empty and all-missing vectors retain their type. Known scalar abbreviations expand exactly, including int64 to integer64. Haven-labelled vectors append +lbl in abbreviation mode and +label otherwise.

Plain matrices and arrays retain their element-type description, and data frames retain their container name without dimensions. Other classed objects use their pillar type description, preserving punctuation and structured labels such as I<int> and list<int> in both modes. An empty or missing custom description falls back to the first class name; errors in custom methods are propagated. A numeric-looking type description does not imply support for numeric summaries.

var_unit() reads actual units/difftime metadata (including hms times), and retains custom unit methods for classes describing themselves as units, drtn or time. Other objects return NA_character_.

See also

Other inspection functions: browse(), codebook(), misstable(), names, variables()

Examples

var_type(1:10)
#> [1] "integer"

var_type(1:10, abbr = TRUE)
#> [1] "int"

var_type(letters)
#> [1] "character"

# Type and unit describe different properties of the same object
elapsed <- as.difftime(c(1, 2, 3), units = "hours")
var_type(elapsed)
#> [1] "duration"

var_unit(elapsed)
#> [1] "hours"