Skip to content

rowwise() + mutate() doesn't respect rowwise-ness when copying a list-col #6302

@DavisVaughan

Description

@DavisVaughan

I don't think this is expected. I would have expected the first to return an x column containing c(1, 2, 3, 4) as a double vector.

library(dplyr)

df <- tibble(x = list(1, 2, 3, 4))
df <- rowwise(df)

# `x` isn't accessed an element at a time
mutate(df, y = x)
#> # A tibble: 4 × 2
#> # Rowwise: 
#>   x         y        
#>   <list>    <list>   
#> 1 <dbl [1]> <dbl [1]>
#> 2 <dbl [1]> <dbl [1]>
#> 3 <dbl [1]> <dbl [1]>
#> 4 <dbl [1]> <dbl [1]>

# `x` is accessed an element at a time
mutate(df, y = x + 1)
#> # A tibble: 4 × 2
#> # Rowwise: 
#>   x             y
#>   <list>    <dbl>
#> 1 <dbl [1]>     2
#> 2 <dbl [1]>     3
#> 3 <dbl [1]>     4
#> 4 <dbl [1]>     5

I think something is off with this section. We can't just use the .data[[name]] result here, we need to unchop it (checking that all elements are size 1 first)

dplyr/R/mutate.R

Lines 323 to 336 in 55dfc1c

# column from the original data
result <- .data[[name]]
chunks <- mask$resolve(name)
}
if (inherits(.data, "rowwise_df") && vec_is_list(result)) {
sizes <- list_sizes(result)
wrong <- which(sizes != 1)
if (length(wrong)) {
# same error as would have been generated by mask$eval_all_mutate()
group <- wrong[1L]
mask$set_current_group(group)
abort(

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavioreach-row ↕️

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions