r-lib / vctrs

Generic programming with typed R vectors
https://vctrs.r-lib.org
Other
282 stars 65 forks source link

`vec_slice()` errors in conjunction with `data.table::data.table()`, `lubridate::period()` and `rbind()` #1905

Open Henning-Lenz opened 6 months ago

Henning-Lenz commented 6 months ago

vctrs package (version 0.6.5 on R 4.3.2) told me to report, so I obey. MWE:

measurements <- data.table::data.table(
  "id" = seq(1, 10),
  "someperiod" = rep(lubridate::period(5), 10)
)

allmeasurements <- rbind(measurements, measurements)

test <- dplyr::arrange(allmeasurements, id)
#> Error in `vec_slice()`:
#> ! Column `someperiod` (size 10) must match the data frame (size 20).
#> ℹ In file 'slice.c' at line 188.
#> ℹ This is an internal error that was detected in the vctrs package.
#>   Please report it at <https://github.com/r-lib/vctrs/issues> with a reprex (<https://tidyverse.org/help/>) and the full backtrace.
#> Backtrace:
#>      ▆
#>   1. ├─dplyr::arrange(allmeasurements, id)
#>   2. ├─dplyr:::arrange.data.frame(allmeasurements, id)
#>   3. │ ├─dplyr::dplyr_row_slice(.data, loc)
#>   4. │ └─dplyr:::dplyr_row_slice.data.frame(.data, loc)
#>   5. │   ├─dplyr::dplyr_reconstruct(vec_slice(data, i), data)
#>   6. │   │ └─dplyr:::dplyr_new_data_frame(data)
#>   7. │   │   ├─row.names %||% .row_names_info(x, type = 0L)
#>   8. │   │   └─base::.row_names_info(x, type = 0L)
#>   9. │   └─vctrs::vec_slice(data, i)
#>  10. └─rlang:::stop_internal_c_lib(...)
#>  11.   └─rlang::abort(message, call = call, .internal = TRUE, .frame = frame)

Created on 2023-12-22 with reprex v2.0.2

While constructing the MWE I found, that it is crucial to

Converting to tibble or data.frame before (but not after) using rbind() produces no error.

So it needs a very special combination of objects to be problematic and perhaps therefore it's originally caused by one of the other packages.

eutwt commented 6 months ago

Looks like rbind.data.table doesn't combine Period columns the same way rbind.data.frame / the Period method of c would, combining the vectors in the object's attributes. This prevents you from subsetting with [.data.table too, e.g. allmeasurements[1,] errors.

data.table issue: https://github.com/Rdatatable/data.table/issues/1454