vctrs (version 0.0.0.9000)

vec_ptype: Find the prototype of a set of vectors

Description

vec_type_common() finds the common type from a set of vectors. vec_ptype() is designed for interative exploration: it wraps vec_type_common() with a custom class that gives a nice output.

Usage

vec_ptype(..., .ptype = NULL)

vec_type_common(..., .ptype = NULL)

vec_type(x)

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

A prototype

Details

This function works by finding the prototype (a zero-length subset) of each input, then using Reduce() and vec_type2() to find the common class. Logical vectors that consist only of NA are converted to the special unspecified type. This is needed because bare NAs should be automatically coercible to any 1d vector.

Examples

Run this code
# NOT RUN {
# Unknown types ------------------------------------------
vec_ptype()
vec_ptype(NA)
vec_ptype(NULL)

# Vectors ------------------------------------------------
vec_ptype(1:10)
vec_ptype(letters)
vec_ptype(TRUE)

vec_ptype(Sys.Date())
vec_ptype(Sys.time())
vec_ptype(factor("a"))
vec_ptype(ordered("a"))

# Matrices -----------------------------------------------
# The prototype of a matrix includes the number of columns
vec_ptype(array(1, dim = c(1, 2)))
vec_ptype(array("x", dim = c(1, 2)))

# Data frames --------------------------------------------
# The prototype of a data frame includes the prototype of
# every column
vec_ptype(iris)

# 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")
)
# }

Run the code above in your browser using DataLab