Recycling describes the concept of repeating elements of one vector to match the size of another. There are two rules that underlie the "tidyverse" recycling rules:

Examples

library(tibble)

Vectors of size 1 are recycled to the size of any other vector:

tibble(x = 1:3, y = 1L)

This includes vectors of size 0:

tibble(x = integer(), y = 1L)

If vectors aren't size 1, they must all be the same size. Otherwise, an error is thrown:

tibble(x = 1:3, y = 4:7)

vctrs backend

Packages in r-lib and the tidyverse generally use [vec_size_common()] and [vec_recycle_common()] as the backends for handling recycling rules.

vec_size_common(1:3, "x")

vec_recycle_common(1:3, "x")

vec_size_common(1:3, c("x", "y"))

Base R recycling rules

The recycling rules described here are stricter than the ones generally used by base R, which are:

We explore the base R rules in detail in vignette("type-size").



Try the vctrs package in your browser

Any scripts or data that you put into this service are public.

vctrs documentation built on May 29, 2024, 11:39 a.m.