tidyverts / tsibble

Tidy Temporal Data Frames and Tools
https://tsibble.tidyverts.org
GNU General Public License v3.0
527 stars 49 forks source link

unexpected result from make_tsibble() #285

Closed EconProf closed 1 year ago

EconProf commented 1 year ago

My expected output for the second example in the documentation is "2020 Oct" "2020 Nov" " 2021 Oct" "2021 Nov". The actual output of "2020 Oct" "2021 Nov" does not seem very useful.

library(tsibble)
#> 
#> Attaching package: 'tsibble'
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, union

make_yearmonth(year = 2021, month = 10:11)
#> <yearmonth[2]>
#> [1] "2021 Oct" "2021 Nov"

make_yearmonth(year = 2020:2021, month = 10:11)
#> <yearmonth[2]>
#> [1] "2020 Oct" "2021 Nov"

Created on 2022-08-21 with reprex v2.0.2

mitchelloharawild commented 1 year ago

This is the result that I would have expected, you're providing 2 years and 2 months and make_yearmonth() combines them into 2 <yearmonth> objects, element by element. The first result applies typical R recycling rules to recycling 2021 into c(2021, 2021) to match the length of 10:11 - https://vctrs.r-lib.org/articles/type-size.html#common-sizes-recycling-rules

To get all combinations of the 2 years and 2 months, you can use a combination generator like tidyr::expand_grid().

library(tsibble)
#> 
#> Attaching package: 'tsibble'
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, union
times <- tidyr::expand_grid(year = 2020:2021, month = 10:11)
make_yearmonth(year = times$year, month = times$month)
#> <yearmonth[4]>
#> [1] "2020 Oct" "2020 Nov" "2021 Oct" "2021 Nov"

Created on 2022-08-22 by the reprex package (v2.0.1)