[Rd] Error in unsplit() with tibbles

Mario Annau m@r|o@@nn@u @end|ng |rom gm@||@com
Sat Nov 21 16:55:26 CET 2020


using the `unsplit()` function with tibbles currently leads to the
following error:

> mtcars_tb <- as_tibble(mtcars, rownames = NULL)
> s <- split(mtcars_tb, mtcars_tb$gear)
> unsplit(s, mtcars_tb$gear)
 Error: Must subset rows with a valid subscript vector.
ℹ Logical subscripts must match the size of the indexed input.
x Input has size 15 but subscript `rep(NA, len)` has size 32.
Run `rlang::last_error()` to see where the error occurred.

Tibble seems to (rightly) complain, that a logical vector has been used for
subsetting which does not have the same length as the data.frame (rows).
Since `NA` is a logical value, the subset should be changed to
`NA_integer_` in `unsplit()`:

> unsplit
function (value, f, drop = FALSE)
    len <- length(if (is.list(f)) f[[1L]] else f)
    if (is.data.frame(value[[1L]])) {
        x <- value[[1L]][rep(*NA_integer_*, len), , drop = FALSE]
        rownames(x) <- unsplit(lapply(value, rownames), f, drop = drop)
    else x <- value[[1L]][rep(NA, len)]
    split(x, f, drop = drop) <- value


	[[alternative HTML version deleted]]

More information about the R-devel mailing list