|
1 | | -#' Recycling rules used by r-lib and the tidyverse |
2 | | -#' |
3 | | -#' @description |
4 | | -#' Recycling describes the concept of repeating elements of one vector to match |
5 | | -#' the size of another. There are two rules that underlie the "tidyverse" |
6 | | -#' recycling rules: |
7 | | -#' |
8 | | -#' - Vectors of size 1 will be recycled to the size of any other vector |
9 | | -#' |
10 | | -#' - Otherwise, all vectors must have the same size |
11 | | -#' |
12 | | -#' @section Examples: |
13 | | -#' |
14 | | -#' ```{r, warning = FALSE, message = FALSE, include = FALSE} |
15 | | -#' library(tibble) |
16 | | -#' ``` |
17 | | -#' |
18 | | -#' Vectors of size 1 are recycled to the size of any other vector: |
19 | | -#' |
20 | | -#' ```{r, comment = "#>"} |
21 | | -#' tibble(x = 1:3, y = 1L) |
22 | | -#' ``` |
23 | | -#' |
24 | | -#' This includes vectors of size 0: |
25 | | -#' |
26 | | -#' ```{r, comment = "#>"} |
27 | | -#' tibble(x = integer(), y = 1L) |
28 | | -#' ``` |
29 | | -#' |
30 | | -#' If vectors aren't size 1, they must all be the same size. Otherwise, an error |
31 | | -#' is thrown: |
32 | | -#' |
33 | | -#' ```{r, comment = "#>", error = TRUE} |
34 | | -#' tibble(x = 1:3, y = 4:7) |
35 | | -#' ``` |
36 | | -#' |
37 | | -#' @section vctrs backend: |
38 | | -#' |
39 | | -#' Packages in r-lib and the tidyverse generally use [vec_size_common()] and |
40 | | -#' [vec_recycle_common()] as the backends for handling recycling rules. |
41 | | -#' |
42 | | -#' - `vec_size_common()` returns the common size of multiple vectors, after |
43 | | -#' applying the recycling rules |
44 | | -#' |
45 | | -#' - `vec_recycle_common()` goes one step further, and actually recycles the |
46 | | -#' vectors to their common size |
47 | | -#' |
48 | | -#' ```{r, comment = "#>", error = TRUE} |
49 | | -#' vec_size_common(1:3, "x") |
50 | | -#' |
51 | | -#' vec_recycle_common(1:3, "x") |
52 | | -#' |
53 | | -#' vec_size_common(1:3, c("x", "y")) |
54 | | -#' ``` |
55 | | -#' |
56 | | -#' @section Base R recycling rules: |
57 | | -#' |
58 | | -#' The recycling rules described here are stricter than the ones generally used |
59 | | -#' by base R, which are: |
60 | | -#' |
61 | | -#' - If any vector is length 0, the output will be length 0 |
62 | | -#' |
63 | | -#' - Otherwise, the output will be length `max(length_x, length_y)`, and a |
64 | | -#' warning will be thrown if the length of the longer vector is not an integer |
65 | | -#' multiple of the length of the shorter vector. |
66 | | -#' |
67 | | -#' We explore the base R rules in detail in `vignette("type-size")`. |
68 | | -#' |
69 | | -#' @name vector_recycling_rules |
70 | | -#' @keywords internal |
71 | | -NULL |
72 | | - |
73 | 1 | #' Vector recycling |
74 | 2 | #' |
75 | 3 | #' `vec_recycle(x, size)` recycles a single vector to a given size. |
76 | 4 | #' `vec_recycle_common(...)` recycles multiple vectors to their common size. All |
77 | | -#' functions obey the [vctrs recycling rules][vector_recycling_rules], and will |
| 5 | +#' functions obey the [vctrs recycling rules][theory-faq-recycling], and will |
78 | 6 | #' throw an error if recycling is not possible. See [vec_size()] for the precise |
79 | 7 | #' definition of size. |
80 | 8 | #' |
|
0 commit comments