larmarange / labelled

Manipulating labelled vectors in R
https://larmarange.github.io/labelled/
GNU General Public License v3.0
75 stars 16 forks source link

No error when assigning a value label to a date vector #156

Closed larmarange closed 8 months ago

larmarange commented 8 months ago
library(labelled)

v <- as.Date("2023-01-01")
l <- as.Date(c("The first day of 2023" = "2023-01-01"))

labelled(v, l)
#> Error in `vec_cast_named()`:
#> ! Can't convert `labels` <date> to match type of `x` <double>.
#> Backtrace:
#>      ▆
#>   1. ├─haven::labelled(v, l)
#>   2. │ └─haven:::vec_cast_named(labels, x, x_arg = "labels", to_arg = "x")
#>   3. │   ├─stats::setNames(vec_cast(x, to, ...), names(x))
#>   4. │   └─vctrs::vec_cast(x, to, ...)
#>   5. └─vctrs (local) `<fn>`()
#>   6.   └─vctrs::vec_default_cast(...)
#>   7.     ├─base::withRestarts(...)
#>   8.     │ └─base (local) withOneRestart(expr, restarts[[1L]])
#>   9.     │   └─base (local) doWithOneRestart(return(expr), restart)
#>  10.     └─vctrs::stop_incompatible_cast(...)
#>  11.       └─vctrs::stop_incompatible_type(...)
#>  12.         └─vctrs:::stop_incompatible(...)
#>  13.           └─vctrs:::stop_vctrs(...)
#>  14.             └─rlang::abort(message, class = c(class, "vctrs_error"), ..., call = call)
val_labels(v) <- l
set_value_labels(v, l)
#> [1] "2023-01-01"

Created on 2024-03-19 with reprex v2.0.2