My pluck solution above only works with lists of length 1! Oops. So that isn't going to help me unlist lists of length > 1.
Here's an updated summary then:
Attempting to reduce a list of Date vectors to a single vector with class Date
# create list of length(2)
dates_list <- list(
lubridate::as_date(
lubridate::ymd("2019-01-01"):lubridate::ymd("2019-01-03")
),
lubridate::as_date(
lubridate::ymd("2020-01-01"):lubridate::ymd("2020-01-03")
)
)
unlist() outputs a numeric vector:
unlist(dates_list)
#> [1] 17897 17898 17899 18262 18263 18264
This fails with length(list) > 1 because pluck needs a single index to pull:
purrr::pluck(dates_list, 1:length(dates_list))
#> Error: Index 1 must have length 1, not 2
Using map to feed pluck one index at a time just gives me the original list back again:
purrr::map(1:length(dates_list), ~ purrr::pluck(dates_list, .))
#> [[1]]
#> [1] "2019-01-01" "2019-01-02" "2019-01-03"
#>
#> [[2]]
#> [1] "2020-01-01" "2020-01-02" "2020-01-03"
… and there’s no "unlisting/vectorising" map_* variant that preserves Date class.
This may be the only way to do it?
Explicitly restoring to `Date class:
lubridate::as_date(
purrr::simplify(dates_list) # obv unlist() would be equivalent here also
)
#> [1] "2019-01-01" "2019-01-02" "2019-01-03" "2020-01-01" "2020-01-02"
#> [6] "2020-01-03"
Created on 2020-10-02 by the reprex package (v0.3.0)