[Rd] Error in unsplit() with tibbles

Peter Dalgaard pd@|gd @end|ng |rom gm@||@com
Sat Nov 21 17:50:13 CET 2020


Yes. Nevermind tibbles, the [rep(NA, len),] construction only happens to work because len will always be >= the number of rows in  value[[1L]], witness

> (1:10)[rep(NA, 20)]
 [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
> (1:20)[rep(NA, 10)]
 [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
> (1:20)[rep(NA_integer_, 10)]
 [1] NA NA NA NA NA NA NA NA NA NA
> (1:10)[rep(NA_integer_, 20)]
 [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA

-pd


> On 21 Nov 2020, at 16:55 , Mario Annau <mario.annau using gmail.com> wrote:
> 
> Hello,
> 
> 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
>    x
> }
> 
> Cheers,
> Mario
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com



More information about the R-devel mailing list