Skip to content

separate_longer_delim errors with "can't recycle input of size X to size Y," #1505

Closed
@llrs

Description

@llrs

In some cases separate_longer_delim doesn't work.

I initially thought that this was due to a different number of values inside each column but in the examples it combines it correctly.
It doesn't seem to come from the delimiter, as it happens both with "," and " ", the order of the rows doesn't affect it too (having first a single value and then one to split).

library("tidyr")
packageVersion("tidyr")
#> [1] '1.3.0'
dfr <- data.frame(a = "a,b,c", b = "A,B")
separate_longer_delim(dfr, c(a, b), delim = ",")
#> Error in `separate_longer_delim()`:
#> ! In row 1, can't recycle input of size 3 to size 2.
#> Backtrace:
#>     ▆
#>  1. └─tidyr::separate_longer_delim(dfr, c(a, b), delim = ",")
#>  2.   └─tidyr:::map_unchop(...)
#>  3.     └─tidyr::unchop(...)
#>  4.       └─tidyr:::df_unchop(...)
#>  5.         └─purrr::reduce(x_sizes, unchop_sizes2, error_call = error_call)
#>  6.           └─purrr:::reduce_impl(.x, .f, ..., .init = .init, .dir = .dir)
#>  7.             └─tidyr (local) fn(out, elt, ...)
#>  8.               └─cli::cli_abort(...)
#>  9.                 └─rlang::abort(...)
dfr2 <- data.frame(a = "a b c", b = "A B")
separate_longer_delim(dfr2, c(a, b), delim = " ")
#> Error in `separate_longer_delim()`:
#> ! In row 1, can't recycle input of size 3 to size 2.
#> Backtrace:
#>     ▆
#>  1. └─tidyr::separate_longer_delim(dfr2, c(a, b), delim = " ")
#>  2.   └─tidyr:::map_unchop(...)
#>  3.     └─tidyr::unchop(...)
#>  4.       └─tidyr:::df_unchop(...)
#>  5.         └─purrr::reduce(x_sizes, unchop_sizes2, error_call = error_call)
#>  6.           └─purrr:::reduce_impl(.x, .f, ..., .init = .init, .dir = .dir)
#>  7.             └─tidyr (local) fn(out, elt, ...)
#>  8.               └─cli::cli_abort(...)
#>  9.                 └─rlang::abort(...)

# From the examples works
df <- tibble(id = 1:3, x = c("x", "x y", "x y z"), y = c("a", "a b", "a b c"))
separate_longer_delim(df, c(x, y), delim = " ")
#> # A tibble: 6 × 3
#>      id x     y    
#>   <int> <chr> <chr>
#> 1     1 x     a    
#> 2     2 x     a    
#> 3     2 y     b    
#> 4     3 x     a    
#> 5     3 y     b    
#> 6     3 z     c
# But not when there is only one row
df2 <- tibble(id = 1, x = "x y z", y = "a b")
separate_longer_delim(df2, c(x, y), delim = " ")
#> Error in `separate_longer_delim()`:
#> ! In row 1, can't recycle input of size 3 to size 2.
#> Backtrace:
#>     ▆
#>  1. └─tidyr::separate_longer_delim(df2, c(x, y), delim = " ")
#>  2.   └─tidyr:::map_unchop(...)
#>  3.     └─tidyr::unchop(...)
#>  4.       └─tidyr:::df_unchop(...)
#>  5.         └─purrr::reduce(x_sizes, unchop_sizes2, error_call = error_call)
#>  6.           └─purrr:::reduce_impl(.x, .f, ..., .init = .init, .dir = .dir)
#>  7.             └─tidyr (local) fn(out, elt, ...)
#>  8.               └─cli::cli_abort(...)
#>  9.                 └─rlang::abort(...)

Created on 2023-06-28 with reprex v2.0.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions