Learn R Programming

⚠️There's a newer version (0.7.1) of this package.Take me there.

vctrs

There are three main goals to the vctrs package, each described in a vignette:

  • To propose vec_size() and vec_ptype() as alternatives to length() and class(); vignette("type-size"). These definitions are paired with a framework for size-recycling and type-coercion. ptype should evoke the notion of a prototype, i.e. the original or typical form of something.

  • To define size- and type-stability as desirable function properties, use them to analyse existing base functions, and to propose better alternatives; vignette("stability"). This work has been particularly motivated by thinking about the ideal properties of c(), ifelse(), and rbind().

  • To provide a new vctr base class that makes it easy to create new S3 vectors; vignette("s3-vector"). vctrs provides methods for many base generics in terms of a few new vctrs generics, making implementation considerably simpler and more robust.

vctrs is a developer-focussed package. Understanding and extending vctrs requires some effort from developers, but should be invisible to most users. It’s our hope that having an underlying theory will mean that users can build up an accurate mental model without explicitly learning the theory. vctrs will typically be used by other packages, making it easy for them to provide new classes of S3 vectors that are supported throughout the tidyverse (and beyond). For that reason, vctrs has few dependencies.

Installation

Install vctrs from CRAN with:

install.packages("vctrs")

Alternatively, if you need the development version, install it with:

# install.packages("devtools")
devtools::install_github("r-lib/vctrs")

Usage

library(vctrs)

# Sizes
str(vec_size_common(1, 1:10))
#>  int 10
str(vec_recycle_common(1, 1:10))
#> List of 2
#>  $ : num [1:10] 1 1 1 1 1 1 1 1 1 1
#>  $ : int [1:10] 1 2 3 4 5 6 7 8 9 10

# Prototypes
str(vec_ptype_common(FALSE, 1L, 2.5))
#>  num(0)
str(vec_cast_common(FALSE, 1L, 2.5))
#> List of 3
#>  $ : num 0
#>  $ : num 1
#>  $ : num 2.5

Motivation

The original motivation for vctrs comes from two separate but related problems. The first problem is that base::c() has rather undesirable behaviour when you mix different S3 vectors:

# combining factors makes integers
c(factor("a"), factor("b"))
#> [1] 1 1

# combining dates and date-times gives incorrect values; also, order matters
dt <- as.Date("2020-01-01")
dttm <- as.POSIXct(dt)

c(dt, dttm)
#> [1] "2020-01-01"    "4321940-06-07"
c(dttm, dt)
#> [1] "2019-12-31 19:00:00 EST" "1970-01-01 00:04:22 EST"

This behaviour arises because c() has dual purposes: as well as its primary duty of combining vectors, it has a secondary duty of stripping attributes. For example, ?POSIXct suggests that you should use c() if you want to reset the timezone.

The second problem is that dplyr::bind_rows() is not extensible by others. Currently, it handles arbitrary S3 classes using heuristics, but these often fail, and it feels like we really need to think through the problem in order to build a principled solution. This intersects with the need to cleanly support more types of data frame columns, including lists of data frames, data frames, and matrices.

Copy Link

Version

Install

install.packages('vctrs')

Monthly Downloads

3,184,560

Version

0.3.6

License

MIT + file LICENSE

Maintainer

Lionel Henry

Last Published

December 17th, 2020

Functions in vctrs (0.3.6)

df_ptype2

Coercion between two data frames
as-is

AsIs S3 class
faq-error-scalar-type

FAQ - Error: Input must be a vector
df_list

Collect columns for data frame construction
data_frame

Construct a data frame
howto-faq-coercion

FAQ - How to implement ptype2 and cast methods?
howto-faq-coercion-data-frame

FAQ - How to implement ptype2 and cast methods? (Data frames)
faq-compatibility-types

FAQ - How is the compatibility of vector types decided?
fields

Tools for accessing the fields of a record.
faq-error-incompatible-attributes

FAQ - Error/Warning: Some attributes are incompatible
partial_frame

Partially specify columns of a data frame
new_date

Date, date-time, and duration S3 classes
new_data_frame

Assemble attributes for data frame construction
new_vctr

vctr (vector) S3 class
list_of

list_of S3 class for homogenous lists
vec_as_index

Convert to an index vector
partial_factor

Partially specify a factor
%0%

Default value for empty vectors
internal-faq-ptype2-identity

Internal FAQ - vec_ptype2(), NULL, and unspecified vectors
vec_arith

Arithmetic operations
howto-faq-fix-scalar-type-error

FAQ - Why isn't my class treated as a vector?
vec_bind

Combine many data frames into one data frame
vec_assert

Assert an argument has known prototype and/or size
vec_ptype_full.integer64

64 bit integers
vec_duplicate

Find duplicated values
vctrs-conditions

Custom conditions for vctrs package
obj_print

print() and str() generics.
vec_cbind_frame_ptype

Frame prototype
vec_chop

Chopping
vec_empty

Is a vector empty
vctrs-data-frame

vctrs methods for data frames
vec_order

Order and sort vectors
maybe_lossy_cast

Lossy cast error
reference-faq-compatibility

FAQ - Is my class compatible with vctrs?
vec_init

Initialize a vector
new_partial

Partial type
name_spec

Name specifications
theory-faq-coercion

FAQ - How does coercion work in vctrs?
vec_count

Count unique values in a vector
vec_compare

Compare two vectors
new_rcrd

rcrd (record) S3 class
vec_data

Extract underlying data
unspecified

A 1d vector of unspecified type
vec-rep

Repeat a vector
vctrs-package

vctrs: Vector Helpers
vec_is_list

Is the object a list?
vec_default_cast

Default cast and ptype2 methods
vec_names

Get or set the names of a vector
vec_list_cast

Cast a list to vector of specific type
vec_as_subscript

Convert to a base subscript type
vec_math

Mathematical operations
vec_repeat

Expand the length of a vector
vec_as_names_legacy

Repair names with legacy method
vec_seq_along

Useful sequences
vec_proxy

Proxy and restore
vec_c

Combine many vectors into one vector
new_factor

Factor/ordered factor S3 class
vec_match

Find matching observations across vectors
vec_cast

Cast a vector to a specified type
s3_register

Register a method for a suggested dependency
vec_as_names

Retrieve and repair names
vec_unique

Find and count unique values
table

Table S3 class
vec_as_location

Create a vector of locations
new_list_of

Create list_of subclass
vec_equal

Test if two vectors are equal
vec_proxy_compare

Comparison and order proxy
vec_fill_missing

Fill in missing values with the previous or following value
vec_proxy_equal

Equality proxy
vec_split

Split a vector into groups
vec_group

Identify groups
vec_ptype

Find the prototype of a set of vectors
vec_ptype2.logical

Find the common type for a pair of vectors
vec_size

Number of observations
vec_identify_runs

Runs
vec_ptype_full

Vector type as a string
vec_type

Deprecated type functions
vec_recycle

Vector recycling
vec_slice

Get or set observations in a vector