r-lib/clock

Cross link to vctrs recycling rules page

Henrik-P opened this issue · 5 comments

From ?year_month_day:

Fields are recycled against each other.

A small example with year of length 1, month of length 2, and day of length 4, where I would expect fields to be recycled against each other. However, it errors:

year_month_day(2023, month = 1:2, day = rep(1:2, each = 2))
# Error in `year_month_day()`:
# ! Can't recycle `year` (size 2) to match `day` (size 4).
# Run `rlang::last_error()` to see where the error occurred.

In general I think "tidyverse" recycling only recycles vector of size 1 (e.g. {vctrs} and {tibble}). This is different than "base" R recycling which also recycles larger vectors. In particular {clock} is built on {vctrs} which follows "tidyverse" recycling conventions:

> vctrs::vec_recycle(1:2, 4)
Error:
! Can't recycle input of size 2 to size 4.
Run `rlang::last_error()` to see where the error occurred.

Thanks a lot for the explanation @trevorld. I was clearly reading the recycling part from a base perspective. Perhaps one could borrow from the tidyverse recycling rules to make the help text a bit clearer:

Vectors [Fields? Time components? (the "fields" are also refered to as "components" in Description)] of size 1 will be recycled to the size of any other vector [field? component?]. Otherwise, all vectors [fields?] must have the same size

@trevorld is right, we use the tidyverse recycling rules here. I'll try and add a cross link to that vctrs recycling rules page in a few places like:

  • Constructors like year_month_day()
  • Arithmetic pages like in #313

Thanks @DavisVaughan. Side note: Interesting that lubridate doesn't conform to the stricter tidyverse recycling rule, at least not in the two x %m+% months(n) examples in my sibling post.

It should, but it is hard to change a 13 year old package. We've been inching towards making it stricter