vec_type() finds the prototype of a single vector. vec_type_common() finds the common type of multiple vectors. vec_ptype() nicely prints the common type of any number of inputs, and is designed for interative exploration.

vec_type(x)

vec_type_common(..., .ptype = NULL)

vec_ptype(...)

Arguments

..., x

Vectors inputs

.ptype

If NULL, the default, the output type is determined by computing the common type across all elements of ....

Alternatively, you can supply .ptype to give the output known type. If getOption("vctrs.no_guessing") is TRUE you must supply this value: this is a convenient way to make production code demand fixed types.

Value

vec_type() and vec_type_common() return a prototype (a size-0 vector)

Details

vec_type_common() first finds the prototype of each input, then finds the common type using vec_type2() and Reduce().

Prototype

A prototype is size 0 vector containing attributes, but no data. Generally, this is just vec_slice(x, 0L), but some inputs require special handling.

For example, the prototype of logical vectors that only contain missing values is the special unspecified type, which can be coerced to any other 1d type. This allows bare NAs to represent missing values for any 1d vector type.

Examples

# Unknown types ------------------------------------------ vec_ptype()
#> Prototype: NULL
vec_ptype(NA)
#> Prototype: logical
vec_ptype(NULL)
#> Prototype: NULL
# Vectors ------------------------------------------------ vec_ptype(1:10)
#> Prototype: integer
vec_ptype(letters)
#> Prototype: character
vec_ptype(TRUE)
#> Prototype: logical
vec_ptype(Sys.Date())
#> Prototype: date
vec_ptype(Sys.time())
#> Prototype: datetime<local>
vec_ptype(factor("a"))
#> Prototype: factor<127a2>
vec_ptype(ordered("a"))
#> Prototype: ordered<127a2>
# Matrices ----------------------------------------------- # The prototype of a matrix includes the number of columns vec_ptype(array(1, dim = c(1, 2)))
#> Prototype: double[,2]
vec_ptype(array("x", dim = c(1, 2)))
#> Prototype: character[,2]
# Data frames -------------------------------------------- # The prototype of a data frame includes the prototype of # every column vec_ptype(iris)
#> Prototype: data.frame< #> Sepal.Length: double #> Sepal.Width : double #> Petal.Length: double #> Petal.Width : double #> Species : factor<12d60> #> >
# The prototype of multiple data frames includes the prototype # of every column that in any data frame vec_ptype( data.frame(x = TRUE), data.frame(y = 2), data.frame(z = "a") )
#> Prototype: <data.frame< #> x: logical #> y: double #> z: factor<127a2> #> >> #> 0. ( , <data.frame<x:logical>> ) = <data.frame<x:logical>> #> 1. ┌ <data.frame<x:logical>> , <data.frame<y:double>> ┐ = <data.frame< #> │ │ x: logical #> │ │ y: double #> └ ┘ >> #> 2. ┌ <data.frame< , <data.frame<z:factor<127a2>>> ┐ = <data.frame< #> │ x: logical │ x: logical #> │ y: double │ y: double #> │ >> │ z: factor<127a2> #> └ ┘ >>