# Prototypes and sizes

### Coercing to common type

It's often important to combine vectors with multiple types. vctrs provides a consistent set of rules for coercion, via vec_type_common(). vec_type_common() possesses the following invariants:

• class(vec_type_common(x, y)) equals class(vec_type_common(y, x)).

• class(vec_type_common(x, vec_type_common(y, z)) equals class(vec_type_common(vec_type_common(x, y))).

• vec_type_common(x, NULL) == x.

i.e. vec_type_common() is both commutative and associative (with respect to class), and has an identity element, NULL, i.e. it's a commutative monoid. This means the underlying implementation is quite simple: we can find the common type of any number of objects by progressively finding the common type of pairs of objects.

Like with vec_type(), the easiest way to explore vec_type_common() is with vec_ptype(): when given multiple inputs, it will print their common prototype. (In other words: program with vec_type_common() but play with vec_ptype().)

• The common type of atomic vectors is computed very similar to rules of base R, except that we do not coerce to character automatically:

vec_ptype(logical(), integer(), double()) vec_ptype(logical(), character())
• Matrices and arrays are automatically broadcast to higher dimensions:

vec_ptype( array(1, c(0, 1)), array(1, c(0, 2)) ) vec_ptype( array(1, c(0, 1)), array(1, c(0, 3)), array(1, c(0, 3, 4)), array(1, c(0, 3, 4, 5)) )

Provided that the dimensions follow the vctrs recycling rules:

vec_ptype( array(1, c(0, 2)), array(1, c(0, 3)) )
• Factors combine levels in the order in which they appear.

fa <- factor("a") fb <- factor("b") levels(vec_type_common(fa, fb)) levels(vec_type_common(fb, fa))
• Combining a date and date-time yields a date-time:

vec_ptype(new_date(), new_datetime())

When combining two date times, the timezone is taken from the first input:

vec_ptype( new_datetime(tzone = "US/Central"), new_datetime(tzone = "Pacific/Auckland") )

Unless it's the local timezone, in which case any explicit time zone will win:

vec_ptype( new_datetime(tzone = ""), new_datetime(tzone = ""), new_datetime(tzone = "Pacific/Auckland") )
• The common type of two data frames is the common type of each column that occurs in both data frames:

vec_ptype( data.frame(x = FALSE), data.frame(x = 1L), data.frame(x = 2.5) )

And the union of the columns that only occur in one:

vec_ptype(data.frame(x = 1, y = 1), data.frame(y = 1, z = 1))

Note that new columns are added on the right-hand side. This is consistent with the way that factor levels and time zones are handled.

### Casting to specified type

vec_type_common() finds the common type of a set of vector. Typically, however, what you want is a set of vectors coerced to that common type. That's the job of vec_cast_common():

str(vec_cast_common( FALSE, 1:5, 2.5 )) str(vec_cast_common( factor("x"), factor("y") )) str(vec_cast_common( data.frame(x = 1), data.frame(y = 1:2) ))

Alternatively, you can cast to a specific prototype using vec_cast():

# Cast succeeds vec_cast(c(1, 2), integer()) # Cast fails vec_cast(c(1.5, 2.5), factor("a"))

If a cast is possible in general (i.e. double -> integer), but information is lost for a specific input (e.g. 1.5 -> 1), it will generate a warning.

vec_cast(c(1.5, 2), integer())

The set of casts is more permissive than the set of coercions and is summarised in the diagram below. Coercions are shown by arrows; possible casts are shown with circles.

knitr::include_graphics("../man/figures/combined.png", dpi = 300)

## Size

vec_size() was motivated by the need to have an invariant that describes the number of "observations" in a data structure. This is particularly important for data frames as it's useful to have some function such that f(data.frame(x)) equals f(x). No base function has this property:

• length(data.frame(x)) equals 1, because the length of a data frame is the number of columns.

• nrow(data.frame(x)) does not equal nrow(x), because nrow() of a vector is NULL.

• NROW(data.frame(x)) equals NROW(x) for vector x, so is almost what we want. But because NROW() is defined in terms of length(), it returns a value for every object, even types that can't go in a data frame, e.g. data.frame(mean) errors even though NROW(mean) is 1.

We define vec_size() as follows:

• It is the length of 1d vectors
• It is the number of rows of data frames, matrices, and arrays.
• It throws error for non vectors.

Given vec_size(), we can give a precise definition of a data frame: a data frame is a list of vectors where every vector has the same size. This has the desirable property of trivially supporting matrix and data frame columns.

### Slicing

vec_slice() is to vec_size() as [ is to length(); i.e. it allows you to select observations, regardless of the dimensionality of the underlying object. vec_slice(x, i) is equivalent to:

• x[i] when x is a vector.
• x[i, , drop = FALSE] when x is a data frame.
• x[i, , , drop = FALSE] when x is a 3d array.
x <- sample(1:10) df <- data.frame(x = x) vec_slice(x, 5:6) vec_slice(df, 5:6)

vec_slice(data.frame(x), i) equals data.frame(vec_slice(x, i)) (modulo variable and row names).

Prototypes are generated with vec_slice(x, 0L); given a prototype, you can generate a vector of given size (filled with NAs) with vec_na()

### Common sizes: recycling rules

Closely related to the definition of size are the recycling rules. The recycling rules determine the size of the output when two vectors of different sizes are combined. In vctrs, the recycling rules are encoded in vec_size_common() which give the common size of a set of vectors:

vec_size_common(1:3, 1:3, 1:3) vec_size_common(1:10, 1) vec_size_common(integer(), 1:3)

vctrs obeys a stricter set of recycling rules than base R, only recycling under two circumstances:

• Vectors of size 1 can be recycled to any size.
• Vectors of any size can be recycled to size 0.

All other size combinations will generate an error. This strictness prevents common mistakes like dest == c("IAH", "HOU")), at the cost of occasionally requiring an explicit calls to rep().

knitr::include_graphics("../man/figures/sizes-recycling.png", dpi = 300)

You can apply the recycling rules in two ways:

• If you have a vector and desired size, use vec_recycle():

vec_recycle(1:3, 3) vec_recycle(1, 10) vec_recycle(1:3, 0)
• If you have multiple vectors and you want to recycle them to the same size, use vec_recycle_common():

 vec_recycle_common(1:3, 1:3) vec_recycle_common(1:10, 1) vec_recycle_common(integer(), 1:3)

## Appendix: recycling in base R

The recycling rules in base R are described in The R Language Definition but are not implemented in a single function, and thus are not applied consistently. Here I give a brief overview of their most common realisation, as well as showing some of the exceptions.

Generally, in base R, when a pair of vectors is not the same length, the shorter vector is recycled to the same length as the longer:

rep(1, 6) + 1 rep(1, 6) + 1:2 rep(1, 6) + 1:3

If the length of the longer vector is not an integer multiple of the length of the shorter, you usually get a warning:

invisible(pmax(1:2, 1:3)) invisible(1:2 + 1:3) invisible(cbind(1:2, 1:3))

But some functions recycle silently:

length(atan2(1:3, 1:2)) length(paste(1:3, 1:2)) length(ifelse(1:3, 1:2, 1:2))

And data.frame() throws an error:

data.frame(1:2, 1:3)

The R language definition states that "any arithmetic operation involving a zero-length vector has a zero-length result". But outside of arithmetic, this rule is not consistently followed:

# length-0 output 1:2 + integer() atan2(1:2, integer()) pmax(1:2, integer()) # dropped cbind(1:2, integer()) # recycled to length of first ifelse(rep(TRUE, 4), integer(), character()) # preserved-ish paste(1:2, integer()) # Errors data.frame(1:2, integer())